MENU
Donate

Free ebook

Free ebook: VBA & Macros Quick Start

Record, edit, and write simple VBA macros to automate repetitive Excel work — no programming background needed.

Get the ebook

Course path

Turn this into a course path

VBA & Macros strings lessons like this one into a structured, advanced-friendly path.

View course path

Free tools

There's a free tool for this

Formula builder, formula explainer, CSV cleaner, and 8 more browser tools — no upload, no sign-up.

Browse free tools

Templates & data

Want to practice this yourself?

Free .xlsx templates and practice datasets — direct download, no sign-up.

Browse templates & datasets

Tip

Looking for something specific?

Search finds functions, articles, courses, and directory entries from one box — the icon lives in the header.

Try search
=ARTICLES

VBA snippets you'll reuse constantly

=YOUTUBE

Watch alongside this article

Public YouTube resources matched to this article's topic, functions, and practice goal.

video / Advanced

The Ultimate Excel Advanced Filters In VBA [Full Training Course]

Excel For Freelancers

Open on YouTube (opens in a new tab)
Excel For Freelancers

Loading preview…

A reference list, not a tutorial — skim for the task you need, copy the snippet, adjust the sheet and range names for your workbook.

Find the last used row

The most common way to find where your data ends, working up from the bottom of the sheet so it ignores blank cells below the data:

Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

Find the last used column

Same idea, working leftward from the far right edge of the sheet:

Dim lastCol As Long
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column

Turn off screen updating for speed

Wrap this around anything that touches a lot of cells — it stops Excel from redrawing after every single action, which is the single biggest speed win available:

Application.ScreenUpdating = False
' ... your code here ...
Application.ScreenUpdating = True

Loop through every file in a folder

Dir returns matching file names one at a time — call it with a pattern first, then with no arguments to get the next match:

Dim folderPath As String, fileName As String
folderPath = "C:\Reports\"
fileName = Dir(folderPath & "*.xlsx")

Do While fileName <> ""
    Debug.Print fileName
    fileName = Dir
Loop

Basic error handling with On Error

Exit Sub before the label keeps the handler from running when nothing went wrong:

Sub SafeDivide()
    On Error GoTo ErrHandler

    Dim result As Double
    result = 10 / Range("A1").Value
    MsgBox result
    Exit Sub

ErrHandler:
    MsgBox "Something went wrong: " & Err.Description
End Sub

A simple InputBox / MsgBox pattern

InputBox returns an empty string if the user clicks Cancel, so check for that before using the result:

Dim userName As String
userName = InputBox("What's your name?", "Greeting")

If userName <> "" Then
    MsgBox "Hello, " & userName & "!"
End If

Copy a range to another sheet

The destination only needs its top-left cell — Excel fills in the rest of the shape automatically:

Sheets("Data").Range("A1:D20").Copy Destination:=Sheets("Summary").Range("A1")

Delete blank rows

Loop backwards from the last row to the first — deleting forward shifts rows up and skips the next one every time you delete:

Dim i As Long, lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

For i = lastRow To 1 Step -1
    If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then
        Rows(i).Delete
    End If
Next i

Check whether a sheet exists before referencing it

Useful before you try to write to a sheet that a previous step might not have created yet:

Function SheetExists(sheetName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = ThisWorkbook.Worksheets(sheetName)
    On Error GoTo 0
    SheetExists = Not ws Is Nothing
End Function

Suppress confirmation prompts for one operation

Some actions — like saving over an existing file, or deleting a sheet — pop up a confirmation dialog. Turn alerts off just for that line, then straight back on:

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
=PRACTICE

Go deeper with this skill

Turn the idea into a repeatable Excel workflow you can explain, rebuild, and review later. For this article, the goal is to practice: Ten short, copy-pasteable VBA snippets for the tasks that come up in almost every macro — last row, folder loops, error handling, and more.

Practice workbook setup

Create a small practice workbook with one raw-data sheet, one working sheet, and one final output sheet.

Practice workflow

  1. Rebuild the example once exactly as described, then repeat it with different labels, dates, or amounts.
  2. Write a short note beside the result explaining what each step is doing and why it matters.
  3. Change one input value and confirm the output updates in the way you expected.
  4. Save a clean copy of the workbook before experimenting further.

Quality checks

  • Inputs, calculations, and final outputs are separated clearly.
  • Headings describe the data without relying on memory or hidden context.
  • The final result can be understood by someone who did not build the workbook.

Common mistakes

  • Mixing raw data and manual adjustments in the same cells.
  • Skipping a quick review after the result looks correct.
  • Building a one-off fix instead of a repeatable workflow.

Next actions

  • Apply the same pattern to a real workbook with 20 to 50 rows of sample data.
  • Add one note that explains when you would not use this approach.

Formula focus: even if this workflow is not formula-heavy, add one check cell that confirms the final output still matches the source data.