VBA Cheatsheet Zero To Mastery V1.01
VBA Cheatsheet Zero To Mastery V1.01
PROGRAMMING
CHEAT SHEET
TRAVIS CUZICK
V1.01
HEEELLLOOOOO!
I’m Andrei Neagoie, Founder and Lead Instructor of the Zero To Mastery Academy.
After working as a Senior Software Developer over the years, I now dedicate 100% of my time to
teaching others valuable software development skills, help them break into the tech industry, and
advance their careers.
In only a few years, over 750,000 students around the world have taken Zero To Mastery courses
and many of them are now working at top tier companies like Apple, Google, Amazon, Tesla, IBM,
Facebook, and Shopify, just to name a few.
This cheat sheet, created by our VBA Programming instructor (Travis Cuzick) provides you with
the key VBA functions and shortcuts that you need to know and remember.
If you want to not only learn VBA programming but also get the exact steps to build your own
projects and get hired as a Data Analyst or Data Scientist, then check out our Career Paths.
Happy Coding!
Andrei
P.S. I also recently wrote a book called Principles For Programmers. You can download the first
five chapters for free here.
Contents
1
Basic Programming Operations
Operation Code
Addition imFour = 2 + 2
Subtraction imZero = 2 - 2
Multiplication imAlsoFour = 2 * 2
2
Integer Division (uses “\” operator) MsgBox(10 \ 3) ‘returns 3
Data Types
Data Type Description Example
3
Logical/Comparison Operators
Operator Symbol Example
Equals = 5=5
If Statements
Type Example Scenario VBA Code
Simple If statement If the value stored in the If val > 1000 Then
variable “val” is greater than
MsgBox(“Large”)
1,000, print the text “Large”.
4
Otherwise, do nothing. End If
If-Else statement If the value stored in the If val > 1000 Then
variable “val” is greater than
MsgBox(“Large”)
1,000, print the text “Large”.
Else
Otherwise, print the text
“Small”. MsgBox(“Small”)
End If
Loops
Type Example Scenario VBA Code
Do
If counter > 5 Then
Exit Do
End If
5
MsgBox (counter)
counter = counter + 1
Loop
Do While Loop Print the first 5 integers to the Dim counter As Integer
screen counter = 1
Do Until Loop Print the first 5 integers to the Dim counter As Integer
screen counter = 1
For Next Loop Print the first 5 integers to the Dim counter As Integer
screen
For counter = 1 To 5
MsgBox (counter)
Next counter
For Each Loop Print the values in cells A1 Dim cell As Range
through A5 to the screen
6
MsgBox (cell.Value)
Next cell
Arrays
Example Scenario VBA Code
myArr(0,0) = “one”
myArr(0,1) = 1
myArr(1,0) = “two”
myArr(1,1) = 2
myArr(2,0) = “three”
myArr(2,1) = 3
7
Print the maximum index (upper bound) Dim myArr(5) As String
of an array
MsgBox(UBound(myArr))
myArr(0) = "one"
myArr(1) = 2
myArr(2) = "three"
myArr = Range("A1:A5").Value
myArr(0) = "one"
myArr(1) = 2
myArr(2) = "three"
Range("A1:C1") = myArr
8
Transfer data from a 3-element array to Dim myArr(2) As Variant
a vertical Excel range
myArr(0) = "one"
myArr(1) = 2
myArr(2) = "three"
Range("A1:A3") =
Application.WorksheetFunction.Transpos
e(myArr)
myArr(0) = "I"
myArr(1) = “am”
myArr(2) = "a"
myArr(3) = "list"
myArr(4) = "of"
myArr(5) = "words"
combinedStr = JOIN(myArr, “ ”)
9
The With Construct
Example Scenario VBA Code (without using With) VBA Code (using With)
10
Set the value of the cell 1 row above, ActiveCell.Offset(-1,-2).Value = “I’m upset
and 2 columns to the left, of the active that I’ve been offset!”
cell
Use the Cells property to set the value Cells(3,4).Value = “Row 3, column 4”
of cell D3
Use the Cells property within a For Next Dim counter As Integer
loop to print the values of the first 5
cells in column A
For counter = 1 To 5
MsgBox (Cells(counter, 1))
Next counter
11
Working With Worksheets
Example Scenario VBA Code
Add a new worksheet and specify its Sheets.Add.Name = “My New Sheet”
name
For Each ws In
ActiveWorkbook.Worksheets
MsgBox (ws.Name)
Next ws
12
Working With Workbooks
Example Scenario VBA Code
13
Useful Keyboard Shortcuts
Press this To do this
Alt+F11 Toggle between the VBA editor and the Excel window
Ctrl+J List the properties and methods for the selected object
Back To Top
14