Macro VBA Cheat Sheet
MS Microsoft Excel skills
Northwest Indian College (NWIC)
2 pag.
Document shared on [Link]
Downloaded by: casey-colley (chillinwiththat@[Link])
VBA CHEAT SHEETS
SHEETS CELLS & RANGES WORKBOOKS
Description VBA Code Description VBA Code Description VBA Code
Activate by Tab Range(“B3”).Activate Activate Workbooks(“Book1”).Activate
Sheets(“Input”).Activate Activate Cell
Name Cells(3,2).Activate
Activate First
Workbooks(1).Activate
Activate by VBA Range(“a1:a3”).Select Opened
[Link]
Code Name Select Range Range(Range(“a1”), Range(“a3”)).Select
Range(Cells(1, 1), Cells(3, 1)).Select Activate Last
Workbooks([Link]).Activate
Activate by Index Opened
Sheets(1).Activate
Position Resize Range(“B3”).Resize(2, 2).Select
Get activate
MsgBox [Link]
Next Sheet [Link] Offset Range(“B3”).Offset(2, 2).Select Workbook
Get ActiveSheet MsgBox [Link] Copy Range(“A1:B3”).Copy Range(“D1”) Get
ThisWorkbook
Select Sheet Sheets(“Input”).Select Cut Range(“A1:B3”).Cut Range(“D1”) MsgBox [Link]
(containing VBA
Code)
Dim ws as Worksheet Range(“A1:B3”).Delete
Set to Variable Delete
Set ws = ActiveSheet Range(“A1:B3”).Delete shift:=xlShiftToLeft Add [Link]
Name / Rename [Link] = “NewName” Range(“A1:A3”).Clear Dim wb As Workbook
Clear Range(“A1:A3”).ClearContents Add to Variable
Set wb = [Link]
Add Sheet [Link] Range(“A1:A3”).ClearFormat
Open [Link](“C:\[Link]”)
Add Sheet and Count Range(“A1:A3”).Count
[Link] = “NewSheet”
Name Dim wb As Workbook
Dim rng as Range Open to Variable Set wb = [Link](“C:\example.
Add Sheet to Dim ws As Worksheet Set to Variable
Set rng = Range(“A1”) xlsm”)
Variable Set ws = [Link]
Range(“A1:A3”).Merge Workbooks(“Book1”).Close
Sheets(“Sheet1”).Copy Merge/UnMerge
Copy Sheet Range(“A1:A3”).UnMerge SaveChanges:=False
Before:=Sheets(“Sheet2”) Close
Workbooks(“Book1”).Close
Dim cell As Range
Sheets(“Sheet1”).visible = False SaveChanges:=True
Hide Sheet or Loop Through
For Each cell In Range(“A1:C3”) Save Workbooks(“Book1”).Save
Sheets(“Sheet1”).visible = xlSheetHidden Cells
MsgBox [Link]
Next cell Save As Workbooks(“Book1”).SaveAs strFileName
Sheets(“Sheet1”).Visible = True
Unhide Sheet or Protect/ Workbooks(1).Protect “password”
Sheets(“Sheet1”).Visible = xlSheetVisible Unprotect Workbooks(1).Unprotect “password”
Very Hide Sheet
Sheets(“Sheet1”).Visible = COLUMNS Set to Dim wb as Workbook
xlSheetVeryHidden Variable Set wb = Workbooks(“Book1”)
Delete Sheet Sheets(“Sheet1”).Delete
Description VBA Code
Dim wb As Workbook
Columns(1).Activate Loop Through
Clear Sheet Sheets(“Sheet1”).[Link] Activate Columns(“a:a”).Activate All Workbook in For Each wb In Workbooks
Range(“a1”).[Link] Workbooks MsgBox [Link]
Unprotect (No
Sheets(“Sheet1”).Unprotect Range(“A1”).EntireColumn. Next wb
Password) Height / Width
ColumnWidth = 30
Unprotect (Pass- If Dir(“C:\[Link]”) = “” Then
Sheets(“Sheet1”).Unprotect “Password” Delete Range(“A1”).[Link] Check Exists MsgBox “File does not exist.”
word)
EndIf
Count Range(“A1”).[Link]
Protect (No
Sheets(“Sheet1”).Protect Copy Closed FileCopy “C:\[Link]”,”C:\[Link]”
Password) Insert Range(“A1”).[Link]
Protect (Pass- dim lCol as long
Sheets(“Sheet1”).Protect “Password” Last lCol = Cells(1, [Link]).End
word)
(xlToLeft).Column
Protect but Allow Sheets(“Sheet1”).Protect
VBA Access UserInterfaceOnly:=True Copy Range(“A:A”).Copy Range(“E:E”)
Range(“A:A”).Copy
Insert
Range(“E:E”).Insert
ROWS ERRORS FILES
Description VBA Code Description VBA Code Description VBA Code
Rows(1).Activate On Error – Stop FileCopy “C:\test\test_old.xlsx”, “C:\test\
Copy File
Activate Rows(“1:1”).Activate code and display On Error Goto 0 test_new.xlsx”
Range(“a1”).[Link] error
Delete File Kill “C:\test\[Link]”
Height / Width Range(“A1”).[Link] = 30 On Error – Skip
error and On Error Resume Next Make Folder MkDir “C:\test\”
Delete Range(“A1”).[Link] continue running
Delete All Files
Kill “C:\test\” & “*.*”
Count Range(“A1”).[Link] On Error – Go From Folder
to a line of code On Error Goto [Label]
Insert Range(“A1”).[Link] Kill “C:\test\” & “*.*”
[Label] Delete Folder
RmDir “C:\test\”
dim lRow as long
Clears (Resets)
Last lRow = Cells([Link], 1).End(xlUp). On Error GoTo –1 Current Directory strPath = CurDir()
Error
Row
ThisWorkbook
Show Error strPath = [Link]
Copy Range(“1:1”).Copy Range(“5:5”) MsgBox [Link] Path
number
Range(“1:1”).Copy strFile = Dir(“C:\test” & “\*”)
Insert Show Description
Range(“5:5”).Insert MsgBox [Link]
of error
Loop Through All Do While Len(strFile) > 0
Function to gen- Files in Folder [Link] strFile
[Link] strFile = Dir
erate own error
Loop
Document shared on [Link]
Downloaded by: casey-colley (chillinwiththat@[Link])
SETTINGS ARRAYS DICTIONARIES
Description VBA Code Description VBA Code Description VBA Code
[Link] = False Dim arr(1 To 3) As Variant Required Tools > References > Microsoft Scripting
Screen Updating
[Link] = True arr(1) = “one” Reference Runtime
Create
arr(2) = “two”
[Link] = False arr(3) = “three” Dim dict As New [Link]
Display Alerts
[Link] = True Create [Link] “”
Dim arr(1 To 3) As Variant [Link] “”
[Link] = False Dim cell As Range, i As Integer
Events
[Link] = True i = LBound(arr) Dim dict As New [Link]
Create From Dim cell As Range
For Each cell In Range(“A1:A3”)
[Link] = Excel Dim key As Integer
i=i+1 Create From
Enable Cancel xlDisabled For Each cell In Range(“A1:A10”)
arr(i) = [Link] Excel
Key [Link] = key = key + 1
Next cell
xlInterrupt [Link] key, [Link]
Dim i as Long Next cell
Text Compare –
Option Compare Text Fori = LBound(arr) To UBound(arr)
Ignore Case Read All Items Add Item [Link] “Key”, “Value”
MsgBox arr(i)
Require Variable Next i
Option Explicit Change Value dict(“Key”) = “Value”
Declaration
Erase Erase arr
Get Value MsgBox dict(“Key”)
Automatic Calcu- [Link] = xlManual
Dim sName As String
lations [Link] = xlAutomatic Array to String If [Link](“Key”) Then
sName = Join(arr, “:”)
Check For Value MsgBox “Exists”
[Link]. End If
Increase Size ReDim Preserve arr(0 To 100)
Background Error BackgroundChecking = False
Checking [Link]. Set Value arr(1) = 22 Remove Item [Link] (“Key”)
BackgroundChecking = True
Remove All Items [Link]
Display Formula [Link] = False
Bar [Link] = True Dim key As Variant
Loop Through For Each key In [Link]
[Link] = False COLLECTIONS Items MsgBox key, dict(key)
Freeze Panes
[Link] = True Next key
Description VBA Code
[Link] = False Count Items [Link]
Full Screen View Dim coll As New Collection
[Link] = True
Create [Link] “one” Make Key Case
[Link] “two” [Link] = vbBinaryCompare
[Link] = Sensitive
PageBreak
xlPageBreakPreview Dim coll As New Collection
Preview Make Key Case
[Link] = xlNormalView Dim cell As Range [Link] = vbTextCompare
Create From Excel For Each cell In Range(“A1:A2”) Insensitive
With ActiveWindow [Link] [Link]
.DisplayHorizontalScrollBar = False Next cell
.DisplayVerticalScrollBar = False
Display Scroll Add Item [Link] “Value”
End WithWith ActiveWindow
Bars
.DisplayHorizontalScrollBar = True
.DisplayVerticalScrollBar = True Add Item Before [Link] “Value”, Before:=1
End With
Add Item After [Link] “Value”, After:=1
[Link] = False Read Item MsgBox coll (1)
Display Status Bar
[Link] = True
Count Items [Link]
[Link] = “I’m working Dim item As Variant
Status Bar
Now!!!” For Each item In coll
Contents Read All Items
[Link] = False MsgBox item
[Link] Next item
Display Work- = False Remove Item [Link] (1)
book Tabs [Link] Remove All Items Set coll = New Collection
= True
[Link] = “AutomateExcel.
UserName
com”
[Link] = “AutomateExcel
App Caption
Model”
Zoom [Link] = 80
Document shared on [Link]
Downloaded by: casey-colley (chillinwiththat@[Link])