Repeat an Excel VBA Macro Until the Last Used Row
Question details
The user wants to run a VBA procedure repeatedly until it has processed the last used Excel row.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A workbook contains a macro that should process rows automatically instead of being called manually for each row.
- Observed behavior
- The desired result is a loop that finds the last relevant row and calls the procedure for each row.
Identify the worksheet and a column that contains an entry for every row you want to process. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm), and enable macros only if you trust the workbook.
Find the last row and pass each row to the macro
Use a row-aware loop when the procedure should process each populated row in a worksheet.
The example uses column A to find the last row and starts at row 2 to skip a header. The called procedure receives the worksheet and row number, so each iteration can work on a different row.
In Excel, press Alt+F11 or select Developer > Visual Basic. In the VBA editor, select Insert > Module.
Paste this code into the standard module, then replace Sheet1 and the example processing line as needed: Option Explicit Public Sub RepeatToLastRow() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim macroName As String Set ws = ThisWorkbook.Worksheets("Sheet1") If Application.WorksheetFunction.CountA(ws.Columns("A")) = 0 Then Exit Sub lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row macroName = "'" & Replace(ThisWorkbook.Name, "'", "''") & "'!ProcessRow" For i = 2 To lastRow Application.Run macroName, ws, i Next i End Sub Public Sub ProcessRow(ByVal ws As Worksheet, ByVal rowNum As Long) Debug.Print ws.Cells(rowNum, "A").Value End Sub
Return to Excel, select Developer > Macros, choose RepeatToLastRow, and click Run. To view the example output, open the VBA editor and select View > Immediate Window.

Resolve a macro-not-found error in Application.Run
Use this check if the loop reports error 1004 because Excel cannot find the called procedure.
Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move everyday spreadsheet work between applications. Excel VBA compatibility and macro availability may vary, so test your specific automation before switching.

Frequently Asked Questions
How does VBA find the last used row in a column?
Use ws.Cells(ws.Rows.Count, "A").End(xlUp).Row to find the last used cell in column A on the worksheet referenced by ws. Change "A" to the column that identifies your data.
Why does the loop keep processing the same row?
A loop that calls a macro without passing or otherwise changing the row may repeat the same operation. Pass the loop variable as an argument, such as Application.Run macroName, ws, i, and use rowNum inside the called procedure.
Why does Option Explicit report that the loop variable is not defined?
Declare the variable before the loop in the VBA editor. For example, add Dim i As Long before For i = 2 To lastRow.
What should I check if Application.Run returns error 1004?
Check the workbook, module, and macro names in the Application.Run call. Make sure the target is a Public procedure in a standard module and that its parameters match the arguments supplied.




