๐Ÿ“Š

VBA Cheat Sheet

Visual Basic for Applications โ€” the macro language built into every copy of Excel, Word, and Access. It's how millions of office workers and analysts automate spreadsheets without ever calling themselves "programmers," and it remains one of the most quietly-used languages in the business world.

๐Ÿ“ Reference page only โ€” VBA only runs inside its host application (Excel, Word, Access), driving that app's specific object model, so a standalone browser sandbox can't reproduce it meaningfully. The Visual Basic Editor built into Excel (Alt+F11, free with any Office license) is where these examples actually run.
Jump to: Your first macroVariablesif / else LoopsWorking with cellsSubs & functions

Your first macro

Sub HelloWorld()
    MsgBox "Hello, world!"
End Sub
๐Ÿ“ Macros live in Sub blocks and are triggered by a button, keyboard shortcut, or the Excel Macros menu โ€” not run from a command line.

Variables

Dim age As Integer
age = 25
Dim name As String
name = "Sam"
Dim price As Double
price = 19.99

if / else

If age >= 18 Then
    MsgBox "Adult"
ElseIf age >= 13 Then
    MsgBox "Teen"
Else
    MsgBox "Child"
End If

Loops

Dim i As Integer
For i = 1 To 5
    MsgBox i
Next i

Dim n As Integer
n = 3
Do While n > 0
    MsgBox n
    n = n - 1
Loop

Working with cells

VBA's real day job โ€” reading, writing, and manipulating spreadsheet data directly.

Range("A1").Value = "Total"
Range("B1").Value = 100

Dim total As Double
total = Range("B1").Value + Range("B2").Value

Dim lastRow As Integer
lastRow = Cells(Rows.Count, 1).End(xlUp).Row   ' find the last used row

Subs & functions

Same split as classic VB โ€” Sub does something, Function returns a value you can use in a formula or another macro.

Function Add(a As Integer, b As Integer) As Integer
    Add = a + b
End Function
โ†’ See the standalone Visual Basic cheat sheet