Excel VBA Loop Through Rows

If you just need to excel vba loop through rows and move on, the boxed answer at the top is all you need. The rest of this page is for when you want to understand why it works in Excel, adapt it to a trickier version, or make it robust enough to hand to a colleague. We keep the opening short on purpose — the depth is here when you want it, not in your way when you don’t.

Exact answer

In Excel: paste Sub LoopThroughRows() into the VBA editor (Alt + F11Insert ▸ Module), press F5 to run, and save as .xlsm to keep the macro.

VBA macro: Loop Through Rows

Sub LoopThroughRows()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Set ws = ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    For i = 1 To lastRow
        ' Example: write row number into column B
        ws.Cells(i, 2).Value = "Row " & i & ": " & ws.Cells(i, 1).Value
    Next i
    MsgBox "Processed " & lastRow & " rows.", vbInformation
End Sub

This is the fundamental VBA row-loop pattern. Replace the comment body with any operation — reading, writing, testing, or formatting cells. Cells(i, 1) is column A of row i; change the second argument for other columns.

How to run this macro

  1. Press Alt + F11 to open the VBA editor.
  2. Insert > Module.
  3. Paste the code above.
  4. Press F5, or close the editor and run it from Developer > Macros.
  5. Save the file as .xlsm so the macro is kept.
Annotated stepsExcel
1

Open Excel and navigate to Developer ▸ Visual Basic (or press Alt + F11).

2

Select Insert ▸ Module from the menu bar inside the VBA editor.

3

Copy and paste Sub LoopThroughRows() into the Module.

4

Click anywhere inside the Sub body and press F5 to run.

5

Close the VBA editor and save the file as .xlsm.

Ctrl+CthenCtrl+Shift+V+Cthen+Ctrl+VPaste values · WindowsMac

What this does

Sub LoopThroughRows() is a ready-to-run VBA macro that handles loop through every row of data using a VBA For loop without manual effort. VBA (Visual Basic for Applications) is Excel's built-in automation language: it can read every cell, loop through thousands of rows, and make decisions in milliseconds. To use it, open the VBA editor with Alt + F11, create a Module, paste the code, and press F5. The Loop Through Rows macro will run immediately — no add-ins, no external tools, just Excel doing exactly what the code tells it to do. The same idea underpins a lot of everyday Excel work, so the few minutes spent getting it right here pay back across every sheet you build afterwards. Treat it as a pattern, not a one-off, and it stops being something you look up and starts being something you reach for. The difference between a quick fix and a sheet you can trust is the extra minute you spend validating “excel vba loop through rows”. Start on a copy or a tiny sample, keep the affected rows visible, and compare the result with the tool above before you touch the real workbook. When this is a ribbon command, the selection matters more than the button: confirm the range, apply the command, then spot-check the output before saving. The point is a workflow that saves repeating the same clicks every week, but the practical win is that someone else can open the file and understand what happened without asking you.

A worked example

Scenario: you have a workbook with 500 rows of sales data and you need to loop through every row of data using a VBA For loop. Without a macro you would scroll through every row manually, which is error-prone and slow. Instead, open the VBA editor (Alt + F11), insert a Module (Insert ▸ Module), paste Sub LoopThroughRows() into the code window, and press F5 to run. The macro processes all 500 rows in under a second, loop through every row of data using a VBA For loop exactly as specified by the code. On a Mac, use Option+F11 to open the editor if Alt+F11 does not respond. Use Sub LoopThroughRows() every time you need to loop through every row of data using a VBA For loop reliably at scale. The macro eliminates the most common sources of error in manual work: missed rows, wrong column references, and inconsistent criteria. Once the code is tested and trusted, you can run it repeatedly with one click — zero cognitive overhead, zero variation in output. If there is any chance you will reuse this, drop it into a small template tab right now: a labelled input area on the left and the formula beside it, checked once against the tool above. Next time the same question comes up, the answer is a single paste away instead of a rebuild from memory.

In Google Sheets

Everything above works in Google Sheets too. Excel and Sheets share the formula syntax used here; only the surrounding menus are arranged differently. That portability is deliberate — learn it once and it follows you between the two tools and across Windows and Mac. Nothing on this page is behind a login: the tool runs entirely in your browser, the formula is shown in full with one-click copy, and the steps work the same on Windows and Mac. That is the whole promise here — the exact answer, a way to prove it on your own numbers, and just enough context to make it stick. The short version of “excel vba loop through rows”: the answer is at the top of this page, the tool proves it on your own numbers, and the sections above explain why it holds so the next variation does not stump you. Excel rewards people who reference cells instead of typing values and who keep inputs separate from formulas, because that is what makes a result you can audit months later. Build it once, deliberately, with the live tool as a check, and you convert a one-off lookup into a reusable skill — which is the whole point of learning the why and not just the what.

Common mistakes

  • Missing Option Explicit at the top of the module — adding it forces VBA to flag typos in variable names immediately, saving time debugging.
  • Not qualifying the range with the worksheet object — using Cells(1,1) without a ws. qualifier acts on whatever sheet is active; if the macro runs from a different sheet it may corrupt data. The pattern `Dim ws As Worksheet: Set ws = ActiveSheet` (or a specific sheet name) pins the action to the right place.
  • Forgetting that macros cannot be undone with Ctrl+Z once they have modified the sheet. Test on a copy of the workbook before running on live data.

Frequently asked questions

How do I run the Loop Through Rows macro?

Press Alt + F11 to open the VBA editor, insert a Module (Insert ▸ Module), paste Sub LoopThroughRows() into the code pane, and press F5. Alternatively close the editor and run it from Developer ▸ Macros ▸ select "LoopThroughRows" ▸ Run.

Why won't my macro run — macros are disabled?

Excel blocks macros by default for files downloaded from the internet. Click the yellow security bar and choose "Enable Content" — that trusts this one workbook and is all that is normally needed. Leave File ▸ Options ▸ Trust Center ▸ Trust Center Settings ▸ Macro Settings on "Disable VBA macros with notification" rather than switching to "Enable all macros", which Microsoft itself marks not recommended. If the bar is red instead of yellow, the file carries Mark of the Web: close Excel, right-click the file, open Properties and tick Unblock. Also ensure the file is saved as .xlsm, not .xlsx.

Can I undo what the Loop Through Rows macro did?

No — VBA actions generally cannot be undone with Ctrl+Z because they bypass Excel's undo stack. Always test Sub LoopThroughRows() on a backup copy of the workbook before running it on live data, or add explicit validation logic inside the macro before the destructive step.