Wauvel

Excel like a finance pro.

The functions, VBA, and shortcuts a finance operator actually uses — curated, with CFO examples. Prefer them pre-built? Grab a ready-made model.

← All VBA snippets

Loop every row down to the last one

The workhorse pattern: find the last used row, then walk down and act on each one.

1What it does

Finds the last row with data in column A (by jumping up from the very bottom of the sheet, so a blank in the middle doesn't cut it short), then loops from row 2 to the end. In the example it flags any row whose column B amount is over $10,000 by writing 'Review' in column C. Swap the test and the action and you have most of everyday VBA.

2The code

Sub LoopRows()
    Dim ws As Worksheet, lastRow As Long, i As Long
    Set ws = ActiveSheet

    ' Find the last used row in column A. Walking UP from the very bottom of the
    ' sheet means a blank row in the middle won't stop you short.
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Row 1 is the header, so start at 2. Example rule: flag any amount in
    ' column B over 10,000 by writing "Review" in column C.
    For i = 2 To lastRow
        If ws.Cells(i, "B").Value > 10000 Then
            ws.Cells(i, "C").Value = "Review"
        End If
    Next i
End Sub

Paste into the Visual Basic Editor (Alt + F11 → Insert → Module), then run and save as macro-enabled (.xlsm).

New to macros? Set up in 5 minutes
  1. 1

    Don't see the Developer tab in the ribbon?

    You don't strictly need it — Alt + F11 opens the editor directly — but it makes running macros easier.
    • Windows: File → Options → Customize Ribbon → tick Developer in the right-hand list → OK.
    • Mac:Excel → Preferences → Ribbon & Toolbar → tick Developer → Save.
  2. 2

    Paste in the code

    Press Alt + F11 to open the Visual Basic editor, then Insert → Moduleand paste the snippet's code into the blank window. Close it with Alt + Q.
  3. 3

    Run it

    Press Alt + F8, pick the macro's name, and click Run — that's it. (Pasted a custom function instead? Just type it into a cell like any built-in: =GrossMargin(B2, B3).)
  4. 4

    Keep the macro — save as .xlsm

    File → Save As → Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx silently drops the code when you save.
  5. 5

    Macros blocked?

    Click Enable Content on the yellow bar. If you downloaded the file, you may first need to right-click it → Properties → tick Unblock → OK, then reopen.

Heads up: macros can't be undone with Ctrl + Z — save a copy before running one that changes your workbook.

3When you use it

  • Walk a data dump and tag, total, or transform each row.
  • Apply a rule to every line item without hard-coding the row count.
  • The skeleton almost every other macro is built inside.

4See it in action

A simulation — press Run to perform what the macro does to a sample workbook.

MonthAmount (B)Flag (C)
2Jan$8,000
3Feb$14,000
4Mar$9,500
5Apr$22,000

5Pitfalls

Hard-coding the last row (For i = 2 To 500) breaks the moment the data grows or shrinks.

Fix: Always compute it: Cells(Rows.Count, "A").End(xlUp).Row.

Column A has a gap, so End(xlUp) stops early.

Fix: Anchor the last-row search on a column that's always filled — an ID or date — not one with blanks.

6No-code alternatives

  • SUMIFS / COUNTIFS If you only need a conditional total or count, a formula beats a loop — no macro at all.
  • Power Query For repeatable transforms of imported data, a query refreshes without code.

Rather not write macros? Wauvel's free tools generate branded, formula-driven Excel for you — no VBA required.

Learn the moves here — or let Wauvel run them on your numbers.

Get my free report →