VBA Cheat Sheet
VBA Cheat Sheet
Contents
Data Types
Logical/Comparison Operators
If Statements
Loops
Arrays
Basic Programming
Operations
Operation Code
Operation Code
Addition imFour = 2 + 2
Subtraction imZero = 2 - 2
Multiplication imAlsoFour = 2 * 2
Data Types
Data
Description Example
Type
Set myWorkbook =
Workbook Worksheet object in Excel
Workbooks(1)
myVariant = “Anything
Variant Unspecified data type
goes!”
Logical/Comparison
Operators
Symbo
Operator Example
l
Equals = 5=5
(5 = 5) Or (5 = 55) = True
If Statements
If the value stored in the variable “val” is If val > 1000 Then:
Simple If
greater than 1,000, print the text “Large”. MsgBox(“Large”)
statement
Otherwise, do nothing. End If
If the value stored in the variable “val” is If val > 1000 Then:
greater than 1,000, print the text “Large”. MsgBox(“Large”)
If-ElseIf-Else If the value stored in the variable “val” is Else If val >= 200 Then:
statement between 200 and 1,000, print the text MsgBox(“Medium”)
“Medium”. > Else: MsgBox(“Small”)
Otherwise, print the text “Small”. End If
Loops
Type Example Scenario VBA Code
Arrays
Example Scenario VBA Code
Dim myArr(5)
Print the 3rd element of an array
myArr(2) = 3
with a 0-based index
MsgBox(myArr(2))
Set the value of the cell 1 row above, and 2 ActiveCell.Offset(-1,-2).Value = “I’m
columns to the left, of the active cell upset that I’ve been offset!”
Dim a As Integer
Dim b As Integer
Use the Cells property within a nested For
For a = 1 To Selection.Rows.Count
Next loop to print the values of all the cells in
For b = 1 To
a 2-dimensional selection (that is, the cells
Selection.Columns.Count
currently selected by the user)
MsgBox (Selection.Cells(a, b))
Next b
Next a
Add a new worksheet and specify its name Sheets.Add.Name = “My New Sheet”
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Loop through all sheets in a workbook
MsgBox (ws.Name)
Next ws
Workbooks(“My Workbook”).Close
Close a workbook and save changes
SaveChanges:=True
Dim wb As Workbook
For Each wb In Application.Workbooks
Loop through all open workbooks
MsgBox (wb.Name)
Next wb
Useful Keyboard
Shortcuts
Press this To do this
Alt+F11 Toggle between the VBA editor and the Excel window
F5 Runs the current procedure (or resumes running it if it has been paused)
Ctrl +
Halts the currently running procedure
Break
Ctrl+J List the properties and methods for the selected object
xample
This example turns off AutoSave and notifies the user that the workbook is
not being automatically saved.
VBCopy
Sub UseAutoSaveOn()
ActiveWorkbook.AutoSaveOn = False
MsgBox "This workbook is being saved automatically: " &
ActiveWorkbook.AutoSaveOn
End Sub
Application.WorkbookBeforeSave
event (Excel)
09/13/2021
Note
In Office 365, Excel supports AutoSave, which enables the user's edits to
be saved automatically and continuously. For more information, see How
AutoSave impacts add-ins and macros to ensure that running code in
response to the WorkbookBeforeSave event functions as intended when
AutoSave is enabled.
Syntax
expression.WorkbookBeforeSave (Wb, SaveAsUI, Cancel)
Parameters
Expand table
Name Required/Optional Data type Description
Wb Required Workboo The workbook.
k
SaveAsU Required Boolean True if the Save As dialog box will be displayed
I due to changes made that need to be saved in the
workbook.
Cancel Required Boolean False when the event occurs. If the event
procedure sets this argument to True, the
workbook isn't saved when the procedure is
finished.
Return value
Nothing
Example
This example prompts the user for a yes or no response before saving any
workbook.
VBCopy
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, _
ByVal SaveAsUI As Boolean, Cancel as Boolean)
a = MsgBox("Do you really want to save the workbook?", vbYesNo)
If a = vbNo Then Cancel = True
End Sub
Syntax
expression.WorkbookDeactivate (Wb)
Parameters
Expand table
Name Required/Optional Data type Description
Wb Required Workbook The workbook.
Return value
Nothing
Example
This example arranges all open windows when a workbook is deactivated.
VBCopy
Private Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
Application.Windows.Arrange xlArrangeStyleTiled
End Sub
09/13/2021
Parameters
Expand table
Return value
Nothing
Remarks
If a chart object or chart sheet is moved from one location to another, the event will not
occur. However, if the chart is moved between a chart object and a chart sheet, the event
will occur because a new chart must be created.
The WorkbookNewChart event will not occur in the following scenarios: copying or pasting a
chart sheet, changing a chart type, changing a chart data source, undoing or redoing
inserting or pasting a chart, and loading a workbook that contains a chart.
Example
The following code example displays a message box when a new chart is added to a
workbook.
VBCopy
ByVal Ch As Chart)
MsgBox ("A new chart was added to: " & Wb.Name & " of type: " & Ch.Type)
End Sub
Application.WorkbookNewSheet
event (Excel)
09/13/2021
Syntax
expression.WorkbookNewSheet (Wb, Sh)
Parameters
Expand table
Name Required/Optional Data type Description
Wb Required Workbook The workbook.
Sh Required Object The new sheet.
Return value
Nothing
Example
This example moves the new sheet to the end of the workbook.
VBCopy
Private Sub App_WorkbookNewSheet(ByVal Wb As Workbook, _
ByVal Sh As Object)
Sh.Move After:=Wb.Sheets(Wb.Sheets.Count)
End Sub
09/13/2021
Syntax
expression.WorkbookOpen (Wb)
Parameters
Expand table
Return value
Nothing
Example
VBCopy
Application.Windows.Arrange xlArrangeStyleTiled
End Sub
Have questions or feedback about Office VBA or this documentation? Please see Office VBA
support and feedback for guidance about the ways you can receive support and provide
feedback.
09/13/2021
Syntax
Parameters
Expand table
Return value
Nothing
Example
This example displays a message stating that the PivotTable report's connection to its source
has been closed. This example assumes that you have declared an object of
type Workbook with events in a class module.
VBCopy
End Sub
Have questions or feedback about Office VBA or this documentation? Please see Office VBA
support and feedback for guidance about the ways you can receive support and provide
feedback.
09/13/2021
Syntax
Parameters
Expand table
Name Required/Optional Data type Description
Return value
Nothing
Example
This example displays a message stating that the PivotTable report's connection to its source
has been opened. This example assumes that you have declared an object of
type Workbook with events in a class module.
VBCopy
End Sub
Have questions or feedback about Office VBA or this documentation? Please see Office VBA
support and feedback for guidance about the ways you can receive support and provide
feedback.
09/13/2021
The WorkbookRowsetComplete event occurs when the user either drills through the
recordset or invokes the rowset action on an OLAP PivotTable.
Syntax
Parameters
Expand table
Name Required/ Data type Description
Optional
Remarks
Because the recordset is created asynchronously, the event allows automation to determine
when the action has been completed. Additionally, because the recordset is created on a
separate sheet, the event needs to be on the workbook level.
Have questions or feedback about Office VBA or this documentation? Please see Office VBA
support and feedback for guidance about the ways you can receive support and provide
feedback.