Sunteți pe pagina 1din 2

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
Sheet1.Activate
Code Name Select Range Range(Range(“a1”), Range(“a3”)).Select
Range(Cells(1, 1), Cells(3, 1)).Select Activate Last
Workbooks(Workbooks.Count).Activate
Activate by Index Opened
Sheets(1).Activate
Position Resize Range(“B3”).Resize(2, 2).Select
Get activate
MsgBox ActiveWorkbook.Name
Next Sheet ActiveSheet.Next.Activate Offset Range(“B3”).Offset(2, 2).Select Workbook

Get ActiveSheet MsgBox ActiveSheet.Name Copy Range(“A1:B3”).Copy Range(“D1”) Get


ThisWorkbook
Select Sheet Sheets(“Input”).Select Cut Range(“A1:B3”).Cut Range(“D1”) MsgBox ThisWorkbook.Name
(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 Workbooks.Add
Name / Rename ActiveSheet.Name = “NewName” Range(“A1:A3”).Clear Dim wb As Workbook
Clear Range(“A1:A3”).ClearContents Add to Variable
Set wb = Workbooks.Add
Add Sheet Sheets.Add Range(“A1:A3”).ClearFormat
Open Workbooks.Open(“C:\example.xlsm”)
Add Sheet and Count Range(“A1:A3”).Count
Sheets.Add.Name = “NewSheet”
Name Dim wb As Workbook
Dim rng as Range Open to Variable Set wb = Workbooks.Open(“C:\example.
Add Sheet to Dim ws As Worksheet Set to Variable
Set rng = Range(“A1”) xlsm”)
Variable Set ws = Sheets.Add
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 cell.Value
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”).Cells.Clear Activate Columns(“a:a”).Activate All Workbook in For Each wb In Workbooks
Range(“a1”).EntireColumn.Activate Workbooks MsgBox wb.Name
Unprotect (No
Sheets(“Sheet1”).Unprotect Range(“A1”).EntireColumn. Next wb
Password) Height / Width
ColumnWidth = 30
Unprotect (Pass- If Dir(“C:\Book1.xlsx”) = “” Then
Sheets(“Sheet1”).Unprotect “Password” Delete Range(“A1”).EntireColumn.Delete Check Exists MsgBox “File does not exist.”
word)
EndIf
Count Range(“A1”).Columns.Count
Protect (No
Sheets(“Sheet1”).Protect Copy Closed FileCopy “C:\file1.xlsx”,”C:\file2.xlsx”
Password) Insert Range(“A1”).EntireColumn.Insert
Protect (Pass- dim lCol as long
Sheets(“Sheet1”).Protect “Password” Last lCol = Cells(1, Columns.Count).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”).EntireRow.Activate error
Delete File Kill “C:\test\example.xlsx”
Height / Width Range(“A1”).EntireRow.RowHeight = 30 On Error – Skip
error and On Error Resume Next Make Folder MkDir “C:\test\”
Delete Range(“A1”).EntireRow.Delete continue running
Delete All Files
Kill “C:\test\” & “*.*”
Count Range(“A1”).Rows.Count On Error – Go From Folder
to a line of code On Error Goto [Label]
Insert Range(“A1”).EntireRow.Inser Kill “C:\test\” & “*.*”
[Label] Delete Folder
RmDir “C:\test\”
dim lRow as long
Clears (Resets)
Last lRow = Cells(Rows.Count, 1).End(xlUp). On Error GoTo –1 Current Directory strPath = CurDir()
Error
Row
ThisWorkbook
Show Error strPath = ThisWorkbook.Path
Copy Range(“1:1”).Copy Range(“5:5”) MsgBox Err.Number Path
number
Range(“1:1”).Copy strFile = Dir(“C:\test” & “\*”)
Insert Show Description
Range(“5:5”).Insert MsgBox Err.Description
of error
Loop Through All Do While Len(strFile) > 0
Function to gen- Files in Folder Debug.Print strFile
Err.Raise strFile = Dir
erate own error
Loop

AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more! automateexcel.com/vba/cheatsheets
SETTINGS ARRAYS DICTIONARIES
Description VBA Code Description VBA Code Description VBA Code
Application.ScreenUpdating = False Dim arr(1 To 3) As Variant Required Tools > References > Microsoft Scripting
Screen Updating
Application.ScreenUpdating = True arr(1) = “one” Reference Runtime
Create
arr(2) = “two”
Application.DisplayAlerts = False arr(3) = “three” Dim dict As New Scripting.Dictionary
Display Alerts
Application.DisplayAlerts = True Create dict.Add “”
Dim arr(1 To 3) As Variant dict.Add “”
Application.EnableEvents = False Dim cell As Range, i As Integer
Events
Application.EnableEvents = True i = LBound(arr) Dim dict As New Scripting.Dictionary
Create From Dim cell As Range
For Each cell In Range(“A1:A3”)
Application.EnableCancelKey = Excel Dim key As Integer
i=i+1 Create From
Enable Cancel xlDisabled For Each cell In Range(“A1:A10”)
arr(i) = cell.value Excel
Key Application.EnableCancelKey = key = key + 1
Next cell
xlInterrupt dict.Add key, cell.value
Dim i as Long Next cell
Text Compare –
Option Compare Text Fori = LBound(arr) To UBound(arr)
Ignore Case Read All Items Add Item dict.Add “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- Application.Calculation = xlManual
Dim sName As String
lations Application.Calculation = xlAutomatic Array to String If dict.Exists(“Key”) Then
sName = Join(arr, “:”)
Check For Value MsgBox “Exists”
Application.ErrorCheckingOptions. End If
Increase Size ReDim Preserve arr(0 To 100)
Background Error BackgroundChecking = False
Checking Application.ErrorCheckingOptions. Set Value arr(1) = 22 Remove Item dict.Remove (“Key”)
BackgroundChecking = True
Remove All Items dict.RemoveAll
Display Formula Application.DisplayFormulaBar = False
Bar Application.DisplayFormulaBar = True Dim key As Variant

ActiveWindow.FreezePanes = False COLLECTIONS Loop Through


Items
For Each key In dict.Keys
MsgBox key, dict(key)
Freeze Panes
ActiveWindow.FreezePanes = True Next key
Description VBA Code
Application.DisplayFullScreen = False Count Items dict.Count
Full Screen View Dim coll As New Collection
Application.DisplayFullScreen = True
Create coll.Add “one” Make Key Case
coll.Add “two” dict.CompareMode = vbBinaryCompare
ActiveWindow.View = Sensitive
PageBreak
xlPageBreakPreview Dim coll As New Collection
Preview Make Key Case
ActiveWindow.View = xlNormalView Dim cell As Range dict.CompareMode = vbTextCompare
Create From Excel For Each cell In Range(“A1:A2”) Insensitive
With ActiveWindow coll.Add cell.value
.DisplayHorizontalScrollBar = False Next cell
.DisplayVerticalScrollBar = False
Display Scroll Add Item coll.Add “Value”
End WithWith ActiveWindow
Bars
.DisplayHorizontalScrollBar = True
.DisplayVerticalScrollBar = True Add Item Before coll.Add “Value”, Before:=1
End With
Add Item After coll.Add “Value”, After:=1
Application.DisplayStatusBar = False Read Item MsgBox coll (1)
Display Status Bar
Application.DisplayStatusBar = True
Count Items coll.Count
Application.StatusBar = “I’m working Dim item As Variant
Status Bar
Now!!!” For Each item In coll
Contents Read All Items
Application.StatusBar = False MsgBox item
ActiveWindow.DisplayWorkbookTabs Next item
Display Work- = False Remove Item coll.Remove (1)
book Tabs ActiveWindow.DisplayWorkbookTabs Remove All Items Set coll = New Collection
= True

Application.UserName = “AutomateExcel.
UserName
com”

Application.Caption = “AutomateExcel
App Caption
Model”

Zoom ActiveWindow.Zoom = 80

AutoMacro:
VBA Add-in with Hundreds of Ready-To-Use Code Examples, Learn More
Code Generators, and much more! automateexcel.com/vba/cheatsheets

S-ar putea să vă placă și