Loop Through Excel Rows with VBA Until a Blank Cell
Question details
Modify an Excel VBA macro to process table rows until it encounters the first empty cell in a key column.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A macro needs to process consecutive records on the FULL REPORT worksheet and stop when the key column is empty.
- Observed behavior
- The desired loop checks each row's key cell before processing and stops at the first blank cell.
Identify the worksheet, the first data row, and the column that determines when processing should stop. Make sure the workbook is saved as a macro-enabled file and that macros are enabled only if you trust the file.
Use a Do While loop to check the key column
Use a row counter to process consecutive rows and stop at the first empty cell in column A.
The example starts at row 2 on the FULL REPORT worksheet. Change the sheet name, starting row, or column number to match your table. Replace the Debug.Print line with the actions your macro should perform on each row.
In Excel, open Developer > Visual Basic, or press Alt+F11 where supported.
Choose Insert > Module and paste this code: Sub ProcessRows() Dim ws As Worksheet Dim r As Long Set ws = ThisWorkbook.Worksheets("FULL REPORT") r = 2 Do While ws.Cells(r, 1).Value <> "" Debug.Print ws.Cells(r, 1).Value ' Add your row-processing code here. r = r + 1 Loop End Sub
In the VBA editor, open View > Immediate Window, then place the cursor inside ProcessRows and choose Run > Run Sub/UserForm. Confirm that the key values appear until the first blank cell.

Run a row-processing macro in WPS Spreadsheets
Use WPS Spreadsheets to work with the table and, where VBA support is available in your WPS installation, run a macro that stops at the first blank key cell. VBA availability can depend on the edition and installed components.
- 1. Open the workbook: Open WPS Spreadsheets and use File > Open to select your Excel workbook.
- 2. Check VBA availability: Look for Developer > Visual Basic or the VBA editor in your WPS installation. If it is unavailable, check your edition and its VBA component requirements.
- 3. Add and run the macro: In the VBA editor, choose Insert > Module, paste the ProcessRows example, and adjust the worksheet name and key column. Run the macro from the editor where supported.
- 4. Save the workbook: Use File > Save As and choose an XLSM macro-enabled format if you need to retain the macro. Verify the macro works in the application where you plan to use it.

Frequently Asked Questions
How do I use a different column to stop the loop?
Change the column argument in ws.Cells(r, 1). For example, ws.Cells(r, 3) checks column C. Update the row-processing code as needed.
What happens if there is a blank cell in the middle of the table?
The Do While test stops at that row, so later rows are not processed. If gaps are valid in your data, use a different stopping condition, such as a known last row.
Does a formula returning an empty string stop the loop?
Yes. The example checks the cell's value against an empty string, so a formula that returns "" stops the loop even though the cell contains a formula.
Why should I avoid Select and ActiveCell in the macro?
Selection can change as the macro runs. Using ws.Cells(r, 1) explicitly identifies the worksheet and row, making the code less dependent on the active cell or sheet.




