Excel VBA Cheat Sheet
Excel VBA Cheat Sheet
Excel VBA Cheat Sheet
Basics Variables
InputBox VariableName = InputBox("Prompt Text") Static Arrays Dim VariableName(Start To End) As DataType
-1.79769313486232E+308 to
Select a Range Range("A1:B10").Select Double
1.79769313486232E+308
Operators Procedures
Concatenation
& Example
operator
Assignment
= Sub PrintMessage()
operator
Increment/decreme
i = i + 1, i += 1, i -= 1, i = i - 1 MsgBox "Hello, World!"
nt operators
Concatenation
assignment s = s & "new text" End Sub
operator
Loops Syntax
Function functionName(argument As DataType) As ReturnType
For Loop ' code to execute
Syntax End Function
For counter_variable = start_value to end_value Example
'Statements to be executed inside the loop Function AddNumbers(num1 As Integer, num2 As Integer) As
Next counter_variable AddNumbers = num1 + num2
Example End Function
For i = 1 To 10
Cells(i, 1).Value = i Conditional Statements
Next i
If Statement
Disable Automatic
Application.Calculation = xlManual
Calculations
Enable Automatic
Task VBA Code Application.Calculation = xlAutomatic
Calculations
Jump to a specific
On Error GoTo [Label] Hide Formula Bar Application.DisplayFormulaBar = False
code section
Display a custom
Err.Raise Show Formula Bar Application.DisplayFormulaBar = True
error
Display error Enter Full Screen
MsgBox Err.Description Application.DisplayFullScreen = True
description View
Display error Switch to PageBreak
MsgBox Err.Number ActiveWindow.View = xlPageBreakPreview
number Preview
Switch to Normal
Reset error handling On Error GoTo -1 ActiveWindow.View = xlNormalView
View
With ActiveWindow .DisplayHorizontalScrollBar
Hide Scroll Bars = False .DisplayVerticalScrollBar = False End
With
Debugging Mode Debug.Print "Message Text" Set Zoom Level ActiveWindow.Zoom = 90 or any desired value
Ctrl + Space Autocompletes a line. Logical Functions If, IfError, And, Or, Not, TRUE, FALSE
Ctrl + End Jumps to the end of a module. Array Functions Array, Split, Join, Filter, Sort
Miscellaneous
Ctrl + Left Arrow Moves the cursor one word to the left. MsgBox, InputBox, Dir, Environ, Shell
Functions
End Moves the cursor to the end of the line.
Moves the cursor to the beginning of the
Home
line.
VBA Commands for Excel
Ctrl + A Selects all texts in the current module.
Stops the execution of the code that's
Ctrl + Break Category Commands
currently running.
Enters the debugging mode and executes Range and Cell
F8 Range(), Cells(), ActiveCell, Offset()
one line of code at a time. Selection
Help: https://excelgraduate.com/vba-cheat-sheet-for-excel/
Excelgraduate
© 2023 excelgraduate.com | All Rights Reserved.