VBA and Macros

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

VBA and Macros

VBA and Macros:


http://analysistabs.com/downloads/
http://rondebruin.nl/

Alt + F11 VB shortcut


Sub Sub procedure
Msgbox "Welcome" displays messages
F5 in macro Runs macro
Alt + F8 in Excel Runs macro
Alt + I + M Adds a module in VB window
Range("G7").Activate Selects a cell/range. Use 'Select' for range and cell
Range("H21:H71").Select
Cells(Row number, Column number).select
Range("C1:F7").select
Range(Cells(1,2), cells(10,5)).select
F8 in VB Executes macro line by line
MsgBox Range("B5").Value Displays a cell's value as message
MsgBox Range("B5").Value & " " & Range("D5").Value & " " & Range("F5").Value & " " & Displays a range's values as message with space
Range("H5").Value
MsgBox Range("B5").Value, vbOKCancel, Range("D5").Value Changes the title of message box and OK-Cancel
options are shown
Range("c8").Value = "=Sum(C2:C7)" Formulas- Sum and Count
Range("E7").Value = "=Count(E2:E7)"
Range("E9").Value = WorksheetFunction.Sum(Range("E2:E7")) Formulas without displaying the formula in the
sheet(directly displays the value)
x = InputBox("Enter your name") Collects name, stores it in a cell and displays it in
Range("A2").Value = x message box
MsgBox ("your name is") & " " & x
(or)
MsgBox ("your name is") & " " & Range("A2").Value
Dim x As Double Declares x and y variables as double
Dim y As Double Receives values for x and y
x = InputBox("enter value") Adds them and displays
y = InputBox("Enter value 2")
MsgBox x + y
Range("b2").Font.Size = 16 Formatting
Range("b2").Font.Bold = True
Range("b2").Font.Underline = True
Range("b2").Font.Italic = True
Range("b2").Font.Color = vbRed
Range("b2").Font.Name = "Comic Sans"
With Range("b13").Font Formatting done easily
.Size = 12
.Color = vbGreen
.Name = "Cambria"
.Bold = True
End With
Selection.Value = "adasdasd" Applies values to the selected cell
Range("B13").Font.Color = RGB(87, 22, 122) Using color codes
Or
Range("B13").Font.color=rgb CTRL+SPACE displays colors as drop down
Selection.Interior.Color = vbBlue Change cell colors
Selection.Borders.LineStyle = x1continuous Borders style and colors
Selection.Borders.Color = rgbBeige
ActiveCell.Value = "Center" Offset - Helps in entering values in other cells without
ActiveCell.Offset(0, -1).Value = "Left" moving the cursor away from active cell
ActiveCell.Offset(1, 1).Value
Range("B2").Activate Equivalent to selecting a table using "Ctrl+Shift+Right'
Range(ActiveCell, ActiveCell.End(xlToRight)).Select and 'Ctrl+Shift+down'
Range(Selection, Selection.End(xlDown)).Select
Range("B16").CurrentRegion.Select Equivalent to selecting a table using 'Ctrl+A'
activesheet.usedrange.select Selects the active sheet's used cells/range
Range("B2").CurrentRegion.Copy Copy and paste entire table
Range("l2").PasteSpecial
Range("b2").CurrentRegion.Copy Range("l9") Copy and paste entire table (Easy method)

VBA and Macros Page 1


Range("b2").CurrentRegion.Copy Range("l9") Copy and paste entire table (Easy method)
Range("B2").CurrentRegion.Copy Copy and Paste only format
Range("l22").PasteSpecial xlPasteFormats
x = InputBox("Enter x") IF condition
y = InputBox("Enter y")
If x > y Then
MsgBox ("X is greater than y")
Else
MsgBox ("Y is greaater than X")
End If
If x = y Then If and Else if condition
MsgBox ("Equal")
ElseIf x > y Then
MsgBox ("x is greater")
Else
MsgBox ("y is greater")
End If
If x > 50 Or y > 50 Then And/Or functions
MsgBox ("Good")
ElseIf x < 50 And y < 50 Then
MsgBox ("Bad")
End If
Space + _ Moves to the next line without error
Select Case ActiveCell.Value Select Case (Multiple IF- in case of grade)
Case Is < 40
ActiveCell.Offset(0, 1) = "Gr E"
Case Is < 60
ActiveCell.Offset(0, 1) = "Gr D"
Case Is < 75
ActiveCell.Offset(0, 1) = "Gr C"
Case Is < 90
ActiveCell.Offset(0, 1) = "Gr B"
Case Else
ActiveCell.Offset(0, 1) = "Gr A"
End Select
x=1 Do until

Do Until x = 6
MsgBox x
x=x+1
Loop
Range("B3").Activate Do until i=13
i=1

Do Until i = 13
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
i=i+1
Range("K3").Activate Do until and IF

Do Until ActiveCell.Offset(0, -1) = ""


If ActiveCell.Offset(0, -1) >= 35000 Then
ActiveCell.Value = "Eligible"
Else
ActiveCell.Value = "Not Eligible"
End If
ActiveCell.Offset(1, 0).Activate
Loop
For i = 1 To 8 For loop for displaying 1 to 12
MsgBox i
Next
x = Range("J2").CurrentRegion.Rows.Count Count the number of rows
MsgBox x - 1
x = Sheets.Count Display the name of the sheets in a column
Range("J8").Activate

For i = 1 To x
ActiveCell.Value = Sheets(i).Name
ActiveCell.Offset(1, 0).Activate
Next
Cells.Select Selects all the cells in the sheet

VBA and Macros Page 2


Cells.Select Selects all the cells in the sheet
Cells.ClearFormats Removes formatting
For Each Cell In Selection For each
Cell.Font.Color = vbRed To change selected cells' font color to red
Next
Dim cl As Range For each
For Each cl In Range("D4:H13") To change selected cells' font size to 24
cl.Font.Size = 24
Next
ActiveSheet.UsedRange.Select Selects only the header row in a sheet
x = ActiveCell.Row
y = Selection.Columns.Count
Range(Cells(x, ActiveCell.Column), Cells(x, y)).Select
Dim sh As Worksheet Display the name of all the sheets in msgbox
For Each sh In Worksheets
MsgBox sh.Name
Next
Sub asdasd() Insert a row above the first row in every sheet and fill
Dim sh As Worksheet "Excel Prodigy"
For Each sh In Worksheets
sh.Activate
Range("A1").Activate
ActiveCell.EntireRow.Insert
ActiveCell.Value = "Excel Prodigy"
Next
End Sub
For Each Sheet In Worksheets Name the sheets in a workbook using some fixed cell
Sheet.Activate
Sheet.Name = Range("D3").Value
Next
Sheets(1).Activate Select a sheet
(or)
Worksheets("Sheetname").Activate
(or)
Sheet5.Activate
Workbooks("Filename.xlsx").Activate Activate a file
MsgBox ActiveSheet.Name Display sheet name and file name and file name with
MsgBox ActiveWorkbook.Name directory
MsgBox ActiveWorkbook.Path
MsgBox ActiveWorkbook.FullName
Msgbox Activesheet.index Displays the position of the activesheet
Sheets.Add After:=Sheets(Sheets.Count), Count:=5 Adds five sheets after the last sheet
ActiveSheet.Copy Copies activesheet to a new workbook
ActiveSheet.Copy Copies activesheet to a new workbook and saves it
ActiveWorkbook.SaveAs "FFF.xlsx" under the name "FFF" in My documents

Activeworkbook.Close Closes the workbook


x = ActiveWorkbook.Path Copies activesheet to a new workbook and saves it
ActiveSheet.Copy under the name "FFF" in the same directory
ActiveWorkbook.SaveAs x & "\FFF.xlsx"
ActiveSheet.Copy After:=Sheets(2) Copies activesheet after sheet 2
x = ActiveWorkbook.Path Save all sheets in a workbook as separate sheets in a
Dim sh As Worksheet folder named "Report" in the current directory
For Each sh In Worksheets
sh.Activate
ActiveSheet.Copy
ActiveWorkbook.SaveAs x & "\Report\" & ActiveSheet.Name
ActiveWorkbook.Close
Next
ph = ActiveWorkbook.Path Fills the name of all excel files in the current directory
Filename = Dir(ph & "\*.xlsx") in the activesheet
Range("E4").Activate
Do Until Filename = ""
ActiveCell.Value = Filename
ActiveCell.Offset(1, 0).Activate
Filename = Dir()
Loop
x = Sheets.Count - 1 Merge contents of all sheets to another sheet in the
For i = 1 To x same workbook

VBA and Macros Page 3


For i = 1 To x same workbook
Sheets(i).Activate (Copies the headings of all the files as well)
Range("A1").Activate
y = ActiveCell.CurrentRegion.Rows.Count
ActiveCell.CurrentRegion.Copy
Worksheets("Sheet1").Activate
ActiveCell.PasteSpecial
ActiveCell.Offset(y, 0).Activate

Next
Worksheets("Sheet1").Activate Merge contents of all sheets to another sheet in the
Range("A2").Activate same workbook
(Doesn't copy the headings and so the first heading
sh = Sheets.Count - 1 has to manually included in the merged sheet before-
For i = 1 To sh hand)
Sheets(i).Activate
x = Range("A2").CurrentRegion.Rows.Count
y = Range("A2").CurrentRegion.Columns.Count

Range(Cells(2, 1), Cells(x, y)).Copy


Worksheets("Sheet1").Activate
ActiveCell.PasteSpecial
ActiveCell.Offset(x - 1, 0).Activate

Next
Path="D:\New files 2012\VBA Training\merge\" Copy and paste all the sheets from all files in a
Path = "C:\Users\Bernard Eugine\Desktop\VBA Macro - Excel Prodigy\Report\" directory after the last sheet of the current workbook

fn = Dir(Path & "*.xlsx")

Do Until fn = ""
Workbooks.Open Path & fn
Sheets.Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
Workbooks(fn).Close

fn = Dir()
Loop

Range("E6").Activate When multiplying two columns, if few cells have NA


On Error GoTo msg1 as values, this program assigns 0 as the product.
Do Until ActiveCell.Offset(0, -1) = ""
ActiveCell.Value = ActiveCell.Offset(0, -1) * ActiveCell.Offset(0, -2) Exit sub helps in skipping the 'msg' label after the
ActiveCell.Offset(1, 0).Activate loop is complete
Loop

Exit Sub
msg1:
ActiveCell.Value = 0
Resume Next
Function servicetax(amt As Double, tax As Double) Creating a function to receive tax rate and amount to
servicetax = amt * tax calculate service tax
End Function
Sheet2.Activate Enter a log of the names of the users opening the file
x = InputBox("Enter your name") in Sheet 2
ActiveCell.Value = x Sheet 2 is Very Hidden
ActiveCell.Offset(0, 1).Value = Now Workbook is auto-saved after antering the name
ActiveCell.Offset(1, 0).Activate

Sheet5.Visible = x1sheetveryhidden
ActiveWorkbook.Save

VBA and Macros Page 4

You might also like