In Excel: paste Sub LoopThroughRows() into the VBA editor (Alt + F11 → Insert ▸ Module), press F5 to run, and save as .xlsm to keep the macro. Excel for the web cannot create, run, or edit VBA macros. Open the workbook in the Excel desktop app to work with macros.
On this page8
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 SubThis 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
- Press Alt + F11 to open the VBA editor.
- Insert > Module.
- Paste the code above.
- Press
F5, or close the editor and run it from Developer > Macros. - Save the file as .xlsm so the macro is kept.
Open Excel and navigate to Developer ▸ Visual Basic (or press Alt + F11).
Select Insert ▸ Module from the menu bar inside the VBA editor.
Copy and paste Sub LoopThroughRows() into the Module.
Click anywhere inside the Sub body and press F5 to run.
Close the VBA editor and save the file as .xlsm.
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. A macro runs in the desktop app's VBA environment, not in the cell grid: the workbook has to be saved in a macro-enabled format such as .xlsm to keep the code, and your macro security settings — or a file that arrived from the internet — can block it until you trust it. Excel for the web cannot run VBA at all. 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 outcome you expected 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 the macro will be shared, add a comment at the top saying what it changes and which sheet it expects, and keep the workbook as .xlsm — saving it as .xlsx removes the code.
In Google Sheets
Google Sheets does not run VBA. A macro written for Excel has to be rewritten in Google Apps Script (Extensions ▸ Apps Script), and an .xlsm file converted to Sheets keeps its cells but drops the code. Nothing on this page is behind a login or a download: the exact answer is at the top, and the detail below it is there for when you need it. That is the whole promise here — the answer first, and just enough context to make it stick. For “excel vba loop through rows”, the checks are about the environment as much as the code: desktop Excel with macros enabled for this file, a macro-enabled workbook and a saved copy to fall back on. Run it once on test data, compare the result with what the code says it does, and only then run it on the real sheet.
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.