0% found this document useful (0 votes)
239 views16 pages

VBA Cheat Sheet + PDF Zero To Mastery

The document provides a cheat sheet for Visual Basic for Applications (VBA) with code examples for basic operations, data types, logical operators, and if statements. It also includes information on joining an online course to learn more advanced VBA topics.

Uploaded by

Amit Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
239 views16 pages

VBA Cheat Sheet + PDF Zero To Mastery

The document provides a cheat sheet for Visual Basic for Applications (VBA) with code examples for basic operations, data types, logical operators, and if statements. It also includes information on joining an online course to learn more advanced VBA topics.

Uploaded by

Amit Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

zerotomastery.io

VBA Cheat Sheet + PDF | Zero To


Mastery
10–13 minutes

If you’ve stumbled across this cheatsheet and are just starting to


learn VBA, you've made a great choice!

VBA was created by Microsoft and is the the programming


language that lives within Excel and the Microsoft Office suite of
products. This has made it very popular with companies solving
analytics challenges and is a great language to learn if you're
interested in becoming a Business Intelligence Analyst or Data
Scientist.

However, if you're stuck in an endless cycle of YouTube tutorials


and want to start building real world projects, become a
professional developer, have fun and actually get hired, then come
join the Zero To Mastery Academy. You'll learn VBA from actual
industry professionals alongside thousands of students in our
private Discord community.

You'll not only learn to become a top 10% VBA Programmer and
Data Analyst by learning advanced topics most courses don't
cover. You'll also build VBA projects, including a complete data
management system with a sleek user interface that you can use
to add to your portfolio and wow employers!

1 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Just want the cheatsheet? No problem! Please enjoy and if you'd


like to submit any suggestions, feel free to email us at
[email protected]

Basic Programming Operations

Operation Code

Declare a Dim myVar As String


variable

Set the value of myVar = “some value”


a variable

Set the value of Set myObj = Range(“B2:C3”)


an object
variable

Gather user userInput = InputBox(“What’s your favorite


input color?”)

Print a message MsgBox(“You shall not pass!”)


to the screen

Execute a Call myMacro


macro from
within another
macro

Use a built-in Application.WorksheetFunction.CountA(“A:A”)


worksheet
function in a
macro

2 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Operation Code

Comment out a ‘VBA will ignore me!


line of code -
note the
apostrophe (‘)

Basic Operations on Data

Operation Code

Addition imFour = 2 + 2

Subtraction imZero = 2 - 2

Multiplication imAlsoFour = 2 * 2

Division (uses “/” operator) MsgBox(10 / 3) ‘returns


3.333333

Integer Division (uses “\” MsgBox(10 \ 3) ‘returns 3


operator)

Concatenation helloWorld = “Hello” & “world”

Data Types

Data Type Description Example

Integer Whole number between 11


-32,768 and 32,767

Long Whole numbers between 1,234,567


-2,147,483,648 and
2,147,483,647

3 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Data Type Description Example

Single Decimal number with 3.141593


seven digits of precision

Double Decimal number with 3.14159265358979


fifteen digits of precision

Date Date values 3/5/2021

String Text data “Hello world”

Boolean Logical true/false values False

Range Range object in Excel Set myRange =


Range(“A1”)

Worksheet Worksheet object in Excel Set mySheet =


Sheets(“Sheet 1”)

Workbook Worksheet object in Excel Set myWorkbook =


Workbooks(1)

Variant Unspecified data type myVariant = “Anything


goes!”

Logical/Comparison Operators

Operator Symbol Example

Equals = 5=5

Not Equals <> 5 <> 55

Greater than > 2>1

Greater than or equal to >= 2 >= 2

4 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Operator Symbol Example

Less than < 4<5

Less than or equal to <= 4 <= 5

And And (5 = 5) And (5 <> 55) = True

(5 = 5) And (5 = 55) = False

(5 <> 5) And (5 = 55) = False

Or Or (5 = 5) Or (5 <> 55) = True

(5 = 5) Or (5 = 55) = True

(5 <> 5) Or (5 = 55) = False

Not Not Not (5 = 5) = False

Not (5 = 55) = True

If Statements

Type Example Scenario VBA Code

Simple If If the value stored in the If val > 1000 Then:


statement variable “val” is greater MsgBox(“Large”)
than 1,000, print the text End If
“Large”.
Otherwise, do nothing.

If-Else If the value stored in the If val > 1000 Then:


statement variable “val” is greater MsgBox(“Large”)
than 1,000, print the text Else: MsgBox(“Small”)
“Large”. End If
Otherwise, print the text

5 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Type Example Scenario VBA Code


“Small”.

If-ElseIf- If the value stored in the If val > 1000 Then:


Else variable “val” is greater MsgBox(“Large”)
statement than 1,000, print the text Else If val >= 200
“Large”. Then:
If the value stored in the MsgBox(“Medium”)
variable “val” is between Else: MsgBox(“Small”)
200 and 1,000, print the End If
text “Medium”. >
Otherwise, print the text
“Small”.

Loops

Type Example Scenario VBA Code

Do Loop Print the first 5 integers to Dim counter As Integer


the screen counter = 1
Do
If counter > 5 Then
Exit Do
End If
MsgBox (counter)
counter = counter + 1
Loop

Do While Print the first 5 integers to Dim counter As Integer


Loop the screen counter = 1
Do While counter <= 5

6 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Type Example Scenario VBA Code


MsgBox (counter)
counter = counter + 1
Loop

Do Until Print the first 5 integers to Dim counter As Integer


Loop the screen counter = 1
Do Until counter > 5
MsgBox (counter)
counter = counter + 1
Loop

For Next Print the first 5 integers to Dim counter As


Loop the screen Integer
For counter = 1 To 5
MsgBox (counter)
Next counter

For Each Print the values in cells A1 Dim cell As Range


Loop through A5 to the screen For Each cell In
Range("A1:A5")
MsgBox (cell.Value)
Next cell

Arrays

Example VBA Code


Scenario

Create an Dim myArr(5) As Integer


empty array
with 5 integer

7 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Example VBA Code


Scenario
elements and
a 0-based
index

Set the value Dim myArr(5) As Integer


at the 3rd myArr(2) = 3
position of an
array with a
0-based
index

Print the 3rd Dim myArr(5)


element of an myArr(2) = 3
array with a MsgBox(myArr(2))
0-based
index

Create an Dim myArr(2, 1) As Variant


empty
2-dimensional
array with 3
rows and 2
columns, that
can
accommodate
multiple data
types

Set the Dim myArr(2, 1) As Variant


values of a myArr(0,0) = “one”

8 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Example VBA Code


Scenario
3x2 array myArr(0,1) = 1
myArr(1,0) = “two”
myArr(1,1) = 2
myArr(2,0) = “three”
myArr(2,1) = 3

Print the Dim myArr(5) As String


maximum MsgBox(UBound(myArr))
index (upper
bound) of an
array

Print all the Dim myArr(2) As Variant


elements of myArr(0) = "one"
an array myArr(1) = 2
using a For myArr(2) = "three"
Next Loop Dim counter As Integer
For counter = 0 To UBound(myArr)
MsgBox (myArr(counter))
Next counter

Transfer data Dim myArr() As Variant


from cells A1 myArr = Range("A1:A5").Value
through A5 to
an array

Transfer data Dim myArr(2) As Variant


from a myArr(0) = "one"
3-element myArr(1) = 2

9 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Example VBA Code


Scenario
array to an myArr(2) = "three"
Excel range Range("A1:C1") = myArr

Transfer data Dim myArr(2) As Variant


from a myArr(0) = "one"
3-element myArr(1) = 2
array to a myArr(2) = "three"
vertical Excel Range("A1:A3") =
range Application.WorksheetFunction.Transpose(myArr)

Use the Dim textStr As String


SPLIT Dim splitStr() As String
function to textStr = "I am a list of words"
convert a text splitStr() = SPLIT(textStr)
string into an
array of
words

Use the JOIN Dim myArr(5) As Variant


function to Dim combinedStr As String
combine an myArr(0) = "I"
array of myArr(1) = “am”
values into a myArr(2) = "a"
single string, myArr(3) = "list"
with those myArr(4) = "of"
values myArr(5) = "words"
separated by combinedStr = JOIN(myArr, “ ”)
spaces

10 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

The With Construct

Example VBA Code (without VBA Code (using


Scenario using With) With)

Format cell A1 Range("A1").Font.Bold = With


as follows: True Range("A1").Font
Bold, Italic, Range("A1").Font.Italic = .Bold = True
Font-style: True .Italic = True
Roboto, Font- Range("A1").Font.Name .Name = "Roboto"
size: 14 = "Roboto" .Size = 14
Range("A1").Font.Size = End With
14

Working With Ranges

Example Scenario VBA Code

Target a single cell using Range(“A1”)


a hard-coded reference

Target multiple cells using Range(“A1:C3”)


a hard-coded reference

Print the value of a cell MsgBox(Range(“A1”).Value)


using a hard-coded
reference

Set the value of a cell Range(“A1”).Value = 11


using a hard-coded
reference

Print the value of the MsgBox(ActiveCell.Value)

11 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Example Scenario VBA Code


active cell

Set the value of the active ActiveCell.Value = 22


cell

Print the value of the cell MsgBox(ActiveCell.Offset(1,2))


1 row below, and 2
columns to the right, of
the active cell

Set the value of the cell 1 ActiveCell.Offset(-1,-2).Value = “I’m


row above, and 2 columns upset that I’ve been offset!”
to the left, of the active
cell

Use the Cells property to MsgBox(Cells(1,1))


print the value of cell A1

Use the Cells property to Cells(3,4).Value = “Row 3, column


set the value of cell D3 4”

Use the Cells property Dim counter As Integer


within a For Next loop to For counter = 1 To 5
print the values of the first MsgBox (Cells(counter, 1))
5 cells in column A Next counter

Use the Cells property Dim a As Integer


within a nested For Next Dim b As Integer
loop to print the values of For a = 1 To Selection.Rows.Count
all the cells in a For b = 1 To
2-dimensional selection Selection.Columns.Count
(that is, the cells currently MsgBox (Selection.Cells(a, b))

12 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Example Scenario VBA Code


selected by the user) Next b
Next a

Select the last cell with Range("A1").End(xlDown).Select


data in a column of values
starting in cell A1

Select all the values in a Range(Range("A1"),


column of data starting in Range("A1").End(xlDown)).Select
cell A1

Working With Worksheets

Example Scenario VBA Code

Activate a sheet by Sheets(“Sheet 1”).Activate


referencing its name

Activate a sheet by Sheets(1).Activate


referencing its index

Print the name of the MsgBox(ActiveSheet.Name)


active worksheet

Add a new worksheet Sheets.Add

Add a new worksheet and Sheets.Add.Name = “My New


specify its name Sheet”

Delete a worksheet Sheets(“My New Sheet”).Delete

Hide a worksheet Sheets(2).visible = False

Unhide a worksheet Sheets(2).visible = True

13 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Example Scenario VBA Code

Loop through all sheets in Dim ws As Worksheet


a workbook For Each ws In
ActiveWorkbook.Worksheets
MsgBox (ws.Name)
Next ws

Working With Workbooks

Example Scenario VBA Code

Activate a workbook Workbooks(“My Workbook”).Activate


by referencing its
name

Activate the first Workbooks(1).Activate


workbook that was
opened, among all
open workbooks

Activate the last Workbooks(Workbooks.Count).Activate


workbook that was
opened, among all
open workbooks

Print the name of MsgBox(ActiveWorkbook.Name)


the active workbook

Print the name of MsgBox(ThisWorkbook.Name)


this workbook (the
one containing the
VBA code)

14 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Example Scenario VBA Code

Add a new Workbooks.Add


workbook

Open a workbook Workbooks.Open(“C:\My Workbook.xlsx”)

Save a workbook Workbooks(“My Workbook”).Save

Close a workbook Workbooks(“My Workbook”).Close


and save changes SaveChanges:=True

Close a workbook Workbooks(“My Workbook”).Close


without saving SaveChanges:=False
changes

Loop through all Dim wb As Workbook


open workbooks For Each wb In Application.Workbooks
MsgBox (wb.Name)
Next wb

Useful Keyboard Shortcuts

Press To do this
this

Alt+F11 Toggle between the VBA editor and the Excel


window

F2 Open the Object browser

F4 Open the Properties window

F5 Runs the current procedure (or resumes running it if


it has been paused)

15 of 16 16-Sep-2023, 7:21 PM
VBA Cheat Sheet + PDF | Zero To Mastery about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fzerotomastery.io%2Fcheatsheets...

Press To do this
this

F8 Starts “debug mode”, in which one line of code is


executed at a time

Ctrl + Halts the currently running procedure


Break

Ctrl+J List the properties and methods for the selected


object

Back To Top

16 of 16 16-Sep-2023, 7:21 PM

You might also like