Common snippets and shortcuts for Excel macros. Bookmark this page.
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
' Do something with ws
Debug.Print ws.Name
Next ws Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
' Column 2 = B. Change to your column. Dim lastCol As Long
lastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
' Row 2. Change to your header row. Dim rng As Range
Set rng = ActiveSheet.Range("A1:C100")
rng.Interior.Color = vbYellow Range("A1:A10").Copy
Range("B1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False MsgBox "Cross-foot check passed!", vbInformation, "Done"
' vbInformation = info icon
' vbExclamation = warning icon
' vbCritical = error icon If IsEmpty(Range("A1").Value) Then
MsgBox "Cell A1 is empty"
End If Range("B2:B100").NumberFormat = "$#,##0.00"
' Or for negatives in parentheses:
' .NumberFormat = "$#,##0.00_);($#,##0.00)" Application.ScreenUpdating = False
' ... your macro code ...
Application.ScreenUpdating = True
' Always turn it back on! Dim count As Long
count = WorksheetFunction.CountA(Range("A:A"))
' CountA counts non-empty cells
' Count counts numbers only