To update on new Automation Techniques using Excel,Ms Access, SQL Server, Power BI and ASP.Net
Friday, September 27, 2013
Thursday, September 26, 2013
Updation of file using VBA
First update data from raw file to final file , second when i do any changes in raw file it will automatic change in final workbook. Keep both files in same folder
https://docs.google.com/file/d/0B23eJ2xd9ODyY2tCdmxOTXh4Snc/edit?usp=sharing
https://docs.google.com/file/d/0B23eJ2xd9ODyZ05PM3lzU2NVRjQ/edit?usp=sharing
https://docs.google.com/file/d/0B23eJ2xd9ODyY2tCdmxOTXh4Snc/edit?usp=sharing
https://docs.google.com/file/d/0B23eJ2xd9ODyZ05PM3lzU2NVRjQ/edit?usp=sharing
Tuesday, September 24, 2013
Using # In If Condition
# is used only for numeric values in VBA
Option Explicit
Dim mydata, tempval, i
Sub getData()
mydata = Application.InputBox("Enter Data", "Data", Type:=2)
For i = 1 To Len(mydata)
If Mid(mydata, i, 1) Like "#" Then
tempval = tempval & Mid(mydata, i, 1)
End If
Next i
MsgBox tempval
tempval = vbNullString
End Sub
Option Explicit
Dim mydata, tempval, i
Sub getData()
mydata = Application.InputBox("Enter Data", "Data", Type:=2)
For i = 1 To Len(mydata)
If Mid(mydata, i, 1) Like "#" Then
tempval = tempval & Mid(mydata, i, 1)
End If
Next i
MsgBox tempval
tempval = vbNullString
End Sub
Finding files name from folders/Subfolders
first of all you will add FSO References
Step for Add:- Tools-Reference-Microsoft Scripting Runtime
and try below code
Sub File_name()
Dim fso As FileSystemObject
Dim fl As File
Dim fldr As Folder
Dim wb As Workbook
Set wb = ThisWorkbook
Set fso = New FileSystemObject
Application.FileDialog(
Application.FileDialog(
Dim fldpath As String
fldpath = Application.FileDialog(
& "\"
Set fldr = fso.GetFolder(fldpath)
i = 2
For Each fl In fldr.Files
Sheet3.Cells(i, "D").Value = fl.Name
i = i + 1
Next fl
End Sub
Friday, September 20, 2013
Form Control vs. ActiveX Control in MS Excel
Difference
|
ActiveX Controls
|
Excel Controls
|
Excel versions
|
97, 2000
|
5, 95, 97, 2000
|
Which toolbar?
|
Control Toolbox
|
Forms
|
Controls available
|
CheckBox, TextBox, CommandButton, OptionButton, ListBox, ComboBox, ToggleButton, SpinButton, ScrollBar, Label, Image
|
Label, GroupBox, Button, CheckBox, OptionButton, ListBox, ComboBox, ScrollBar, Spinner
|
Macro code storage
|
In the code module for the Sheet
|
In any standard VBA module
|
Macro name
|
Corresponds to the control name (e.g., CommandButton1_Click)
|
Any name you specify.
|
Correspond to...
|
UserForm controls
|
Dialog Sheet controls
|
Customization
|
Extensive, using the Properties box
|
Minimal
|
Respond to events
|
Yes
|
Click or Change events only
|
Convert Time Zone through VBA
Option Explicit
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(31) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(31) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
'Purpose : Converts local time to GMT.
'Inputs : dtLocalDate The local data time to return as GMT.
'Outputs : Returns the local time in GMT.
'Author : Andrew Baker
'Date : 13/11/2002 10:16
'Notes :
'Revisions :
Public Function ConvertLocalToGMT(dtLocalDate As Date) As Date
Dim lSecsDiff As Long
'Get the GMT time diff
lSecsDiff = GetLocalToGMTDifference()
'Return the time in GMT
ConvertLocalToGMT = DateAdd("s", -lSecsDiff, dtLocalDate)
End Function
'Purpose : Converts GMT time to local time.
'Inputs : dtLocalDate The GMT data time to return as local time.
'Outputs : Returns GMT as local time.
'Author : Andrew Baker
'Date : 13/11/2002 10:16
'Notes :
'Revisions :
Public Function ConvertGMTToLocal(gmtTime As Date) As Date
Dim Differerence As Long
Differerence = GetLocalToGMTDifference()
ConvertGMTToLocal = DateAdd("s", Differerence, gmtTime)
End Function
'Purpose : Returns the time lDiff between local and GMT (secs).
'Inputs : dtLocalDate The local data time to return as GMT.
'Outputs : Returns the local time in GMT.
'Author : Andrew Baker
'Date : 13/11/2002 10:16
'Notes : A positive number indicates your ahead of GMT.
'Revisions :
Public Function GetLocalToGMTDifference() As Long
Const TIME_ZONE_ID_INVALID& = &HFFFFFFFF
Const TIME_ZONE_ID_STANDARD& = 1
Const TIME_ZONE_ID_UNKNOWN& = 0
Const TIME_ZONE_ID_DAYLIGHT& = 2
Dim tTimeZoneInf As TIME_ZONE_INFORMATION
Dim lRet As Long
Dim lDiff As Long
'Get time zone info
lRet = GetTimeZoneInformation(tTimeZoneInf)
'Convert diff to secs
lDiff = -tTimeZoneInf.Bias * 60
GetLocalToGMTDifference = lDiff
'Check if we are in daylight saving time.
If lRet = TIME_ZONE_ID_DAYLIGHT& Then
'In daylight savings, apply the bias
If tTimeZoneInf.DaylightDate.wMonth <> 0 Then
'if tTimeZoneInf.DaylightDate.wMonth = 0 then the daylight
'saving time change doesn't occur
GetLocalToGMTDifference = lDiff - tTimeZoneInf.DaylightBias * 60
End If
End If
End Function
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(31) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(31) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
'Purpose : Converts local time to GMT.
'Inputs : dtLocalDate The local data time to return as GMT.
'Outputs : Returns the local time in GMT.
'Author : Andrew Baker
'Date : 13/11/2002 10:16
'Notes :
'Revisions :
Public Function ConvertLocalToGMT(dtLocalDate As Date) As Date
Dim lSecsDiff As Long
'Get the GMT time diff
lSecsDiff = GetLocalToGMTDifference()
'Return the time in GMT
ConvertLocalToGMT = DateAdd("s", -lSecsDiff, dtLocalDate)
End Function
'Purpose : Converts GMT time to local time.
'Inputs : dtLocalDate The GMT data time to return as local time.
'Outputs : Returns GMT as local time.
'Author : Andrew Baker
'Date : 13/11/2002 10:16
'Notes :
'Revisions :
Public Function ConvertGMTToLocal(gmtTime As Date) As Date
Dim Differerence As Long
Differerence = GetLocalToGMTDifference()
ConvertGMTToLocal = DateAdd("s", Differerence, gmtTime)
End Function
'Purpose : Returns the time lDiff between local and GMT (secs).
'Inputs : dtLocalDate The local data time to return as GMT.
'Outputs : Returns the local time in GMT.
'Author : Andrew Baker
'Date : 13/11/2002 10:16
'Notes : A positive number indicates your ahead of GMT.
'Revisions :
Public Function GetLocalToGMTDifference() As Long
Const TIME_ZONE_ID_INVALID& = &HFFFFFFFF
Const TIME_ZONE_ID_STANDARD& = 1
Const TIME_ZONE_ID_UNKNOWN& = 0
Const TIME_ZONE_ID_DAYLIGHT& = 2
Dim tTimeZoneInf As TIME_ZONE_INFORMATION
Dim lRet As Long
Dim lDiff As Long
'Get time zone info
lRet = GetTimeZoneInformation(tTimeZoneInf)
'Convert diff to secs
lDiff = -tTimeZoneInf.Bias * 60
GetLocalToGMTDifference = lDiff
'Check if we are in daylight saving time.
If lRet = TIME_ZONE_ID_DAYLIGHT& Then
'In daylight savings, apply the bias
If tTimeZoneInf.DaylightDate.wMonth <> 0 Then
'if tTimeZoneInf.DaylightDate.wMonth = 0 then the daylight
'saving time change doesn't occur
GetLocalToGMTDifference = lDiff - tTimeZoneInf.DaylightBias * 60
End If
End If
End Function
Tuesday, September 17, 2013
Data Validation Using VBA
Please find the attachment
https://docs.google.com/file/d/0B23eJ2xd9ODyemRISnRSMmQ5LUk/edit?usp=sharing
https://docs.google.com/file/d/0B23eJ2xd9ODyemRISnRSMmQ5LUk/edit?usp=sharing
Example of Select Case in VBA
Dim rng As Range, totalsalary As Long
Sub calculatesalary()
Set rng = Application.InputBox("Select Range", "salary", Type:=8)
For Each cell In rng
Select Case cell.Value
Case Is <= 900
totalsalary = cell.Value + (cell.Value * 0.1)
Case 901 To 1000
totalsalary = cell.Value + (cell.Value * 0.125)
Case Is > 1000
totalsalary = cell.Value + (cell.Value * 0.15)
End Select
cell.Offset(, 1) = totalsalary
Next
End Sub
Monday, September 16, 2013
Using Enumeration In VBA
VBA code to develop a function to calculate salary with allowance
Public Enum commission
grade1 = 100
grade2 = 125
grade3 = 150
End Enum
Function Calculatesalary(ByVal rng As Range) As Long
Dim salary, totalsalary As Long, myrng As Range
Set myrng = rng
salary = CLng(myrng.Value)
Select Case (salary <= 900)
Case True
totalsalary = salary + salary * ((commission.grade1) / 1000)
Case Else
Select Case (900 < salary <= 1000)
Case True
totalsalary = salary + salary * ((commission.grade2) / 1000)
Case Else
Select Case (1000 < salary)
Case True
totalsalary = salary + salary * ((commission.grade3) / 1000)
End Select
End Select
End Select
Calculatesalary = totalsalary
totalsalary = 0
End Function
For details read
http://www.cpearson.com/excel/Enums.aspx
Sunday, September 15, 2013
Count of Vowels In A String USING VBA
Option Explicit
Dim i As Integer, j As Integer
Dim myval As String
Sub countofVowels()
myval = Application.InputBox("Enter a word", "Word", Type:=2)
For i = 1 To Len(myval)
If LCase(Mid(myval, i, 1)) Like "[a,e,i,o,u]" Then
j = j + 1
End If
Next
MsgBox "Total count of Vowels" & j
i = 0
j = 0
End Sub
Dim i As Integer, j As Integer
Dim myval As String
Sub countofVowels()
myval = Application.InputBox("Enter a word", "Word", Type:=2)
For i = 1 To Len(myval)
If LCase(Mid(myval, i, 1)) Like "[a,e,i,o,u]" Then
j = j + 1
End If
Next
MsgBox "Total count of Vowels" & j
i = 0
j = 0
End Sub
Saturday, September 14, 2013
Insert Multiple Row before Unique Value through VBA
Region | Product | Grand_Total |
ANZ | Tech | 159 |
ANZ | OFM | 70 |
ANZ | OFM | 70 |
TC | HCIL | 41 |
TC | HCIL | 297 |
ASEAN | Apps | 80 |
ASEAN | Apps | 587 |
ASEAN | Systems | 350 |
ASEAN | Apps | 34 |
MGI | ORC | 600 |
MGI | ORC | 658 |
MGI | ORC | 750 |
DL | ORG | 340 |
DL | ORG | 123 |
DL | ORG | 107 |
GC | Systems | 161 |
GC | Apps | 83 |
GC | Apps | 83 |
HR | CI | 611 |
HR | CI | 113 |
HR | CI | 596 |
IN | Tech | 551 |
IN | Tech | 832 |
IN | Tech | 66 |
KR | Tech | 275 |
KR | OFM | 87 |
KR | OFM | 81 |
MP | HCL | 665 |
MP | HCL | 579 |
HP | MPGC | 662 |
HP | MPGC | 672 |
HP | MPGC | 319 |
HP | MPGC | 772 |
HP | MPGC | 129 |
For example if you want to insert a row after every unique region here is code
Option Explicit
Dim mycoll As Collection, strrow As String, finalstrrow As String
Dim myrng As Range, rowcount As Long, cell, i As Integer
Sub insertRowafterUnique()
rowcount = ThisWorkbook.Sheets(1).Range("A1").End(xlDown).Row
Set mycoll = New Collection
Set myrng = ThisWorkbook.Sheets(1).Range("A2:A" & rowcount)
strrow = vbNullString
On Error Resume Next
For Each cell In myrng
mycoll.Add cell, CStr(cell)
Next
For i = 2 To rowcount
If ThisWorkbook.Sheets(1).Range("A" & i) <> ThisWorkbook.Sheets(1).Range("A" & (i + 1)) Then
strrow = strrow & (i + 1) & ":" & (i + 1) & ","
End If
Next
finalstrrow = Left(strrow, Len(strrow) - 1)
ThisWorkbook.Sheets(1).Range(finalstrrow).EntireRow.Insert
End Sub
Dim mycoll As Collection, strrow As String, finalstrrow As String
Dim myrng As Range, rowcount As Long, cell, i As Integer
Sub insertRowafterUnique()
rowcount = ThisWorkbook.Sheets(1).Range("A1").End(xlDown).Row
Set mycoll = New Collection
Set myrng = ThisWorkbook.Sheets(1).Range("A2:A" & rowcount)
strrow = vbNullString
On Error Resume Next
For Each cell In myrng
mycoll.Add cell, CStr(cell)
Next
For i = 2 To rowcount
If ThisWorkbook.Sheets(1).Range("A" & i) <> ThisWorkbook.Sheets(1).Range("A" & (i + 1)) Then
strrow = strrow & (i + 1) & ":" & (i + 1) & ","
End If
Next
finalstrrow = Left(strrow, Len(strrow) - 1)
ThisWorkbook.Sheets(1).Range(finalstrrow).EntireRow.Insert
End Sub
Thursday, September 12, 2013
Find vs Search
Find
- Case Sensitive
- Can't using Wildcard
Seach
- Not Case Sensitive
- Can Using Wildcard
- Case Sensitive
- Can't using Wildcard
Seach
- Not Case Sensitive
- Can Using Wildcard
Create Validation by Removing Duplicates
A |
B |
C |
D |
B |
C |
A |
F |
G |
D |
A |
Option Explicit
Dim rowcount As Long
Dim myarray As New Collection, tempval As String, finaltempval As String
Sub addValidation()
On Error Resume Next
rowcount = Sheets(2).Range("a3").End(xlDown).Row
For i = 3 To rowcount
tempval = vbNullString
myarray.Add Range("A" & i), Range("A" & i)
Next i
For j = 1 To myarray.Count
tempval = tempval & myarray(j) & ","
Next j
finaltempval = Mid(tempval, 1, Len(tempval) - 1)
With Range("B1").Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=finaltempval
End With
finaltempval = vbNullString
End Sub
Dim rowcount As Long
Dim myarray As New Collection, tempval As String, finaltempval As String
Sub addValidation()
On Error Resume Next
rowcount = Sheets(2).Range("a3").End(xlDown).Row
For i = 3 To rowcount
tempval = vbNullString
myarray.Add Range("A" & i), Range("A" & i)
Next i
For j = 1 To myarray.Count
tempval = tempval & myarray(j) & ","
Next j
finaltempval = Mid(tempval, 1, Len(tempval) - 1)
With Range("B1").Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=finaltempval
End With
finaltempval = vbNullString
End Sub
Wednesday, September 11, 2013
Hide Formula in Workbook
Goto format of cell - protection- check Hidden - OK....after
this protect your sheet...
Friday, September 6, 2013
Setting Up Expiry Date for Workbook
Private Sub Workbook_Open()
Dim ExpDate As Date
ExpDate = #8/17/2013 10:00:00 AM#
If Now > ExpDate Then ThisWorkbook.Close
MsgBox "This workbook will expired at " & Format(ExpDate, "dd mmmm yyyy hh:mm:ss AM/PM")
End Sub
Dim ExpDate As Date
ExpDate = #8/17/2013 10:00:00 AM#
If Now > ExpDate Then ThisWorkbook.Close
MsgBox "This workbook will expired at " & Format(ExpDate, "dd mmmm yyyy hh:mm:ss AM/PM")
End Sub
Thursday, September 5, 2013
VBA Automation for MS Excel File
In Master Sheet there are three cells in attached sheet G1,G2 & J2. When I will run macro it should check that whether values are entered in these cells. If any one of cells are blank, cell should be RED and give message that values are not entered. and then it should ask Do you want to enter data now, if yes input box for entering value.
Option Explicit
Dim birth As Range, PANNo As Range, empName As Range, userInput
Sub validateData()
Application.ScreenUpdating = False
Set birth = [G2]
Set PANNo = [J2]
Set empName = [G1]
If IsEmpty(empName) Then
[empName].Interior.ColorIndex = 3
[empName].Interior.Pattern = xlSolid
If MsgBox("Employee Name is empty;Please enter now", vbYesNo) = vbYes Then
userInput = Application.InputBox("Please Enter Employee name", Type:=2)
If Len(userInput) > 0 Then
[empName].Value = userInput
[empName].Interior.ColorIndex = 2
End If
End If
End If
If IsEmpty(birth) Then
[birth].Interior.ColorIndex = 3
[birth].Interior.Pattern = xlSolid
If MsgBox("Date of Birth is empty; Please enter now", vbYesNo) = vbYes Then
userInput = vbNullString
userInput = Application.InputBox("Enter DOB", "DOB in mm/dd/yyyy format", Default:=Format(Date, "mm/dd/yyyy"), Type:=2)
If IsDate(userInput) Then
[birth].Value = userInput
[birth].Interior.ColorIndex = 2
End If
End If
End If
If IsEmpty(PANNo) Then
[PANNo].Interior.ColorIndex = 3
[PANNo].Interior.Pattern = xlSolid
If MsgBox("PAN No. is empty;Please enter now", vbYesNo) = vbYes Then
userInput = vbNullString
userInput = Application.InputBox("Enter PAN No.", "PAN No.", Type:=2)
If Len(userInput) = 10 Then
[PANNo].Value = userInput
[PANNo].Interior.ColorIndex = 2
End If
End If
End If
End Sub
https://docs.google.com/file/d/0B23eJ2xd9ODybDdROVo4dUxLVjA/edit?usp=sharing
Option Explicit
Dim birth As Range, PANNo As Range, empName As Range, userInput
Sub validateData()
Application.ScreenUpdating = False
Set birth = [G2]
Set PANNo = [J2]
Set empName = [G1]
If IsEmpty(empName) Then
[empName].Interior.ColorIndex = 3
[empName].Interior.Pattern = xlSolid
If MsgBox("Employee Name is empty;Please enter now", vbYesNo) = vbYes Then
userInput = Application.InputBox("Please Enter Employee name", Type:=2)
If Len(userInput) > 0 Then
[empName].Value = userInput
[empName].Interior.ColorIndex = 2
End If
End If
End If
If IsEmpty(birth) Then
[birth].Interior.ColorIndex = 3
[birth].Interior.Pattern = xlSolid
If MsgBox("Date of Birth is empty; Please enter now", vbYesNo) = vbYes Then
userInput = vbNullString
userInput = Application.InputBox("Enter DOB", "DOB in mm/dd/yyyy format", Default:=Format(Date, "mm/dd/yyyy"), Type:=2)
If IsDate(userInput) Then
[birth].Value = userInput
[birth].Interior.ColorIndex = 2
End If
End If
End If
If IsEmpty(PANNo) Then
[PANNo].Interior.ColorIndex = 3
[PANNo].Interior.Pattern = xlSolid
If MsgBox("PAN No. is empty;Please enter now", vbYesNo) = vbYes Then
userInput = vbNullString
userInput = Application.InputBox("Enter PAN No.", "PAN No.", Type:=2)
If Len(userInput) = 10 Then
[PANNo].Value = userInput
[PANNo].Interior.ColorIndex = 2
End If
End If
End If
End Sub
https://docs.google.com/file/d/0B23eJ2xd9ODybDdROVo4dUxLVjA/edit?usp=sharing
Subscribe to:
Posts (Atom)