VBA & Macro Problems

Excel VBA: Skip Blank Cells and Find the Next Nonblank Cell

Chanuka GeekiyanageChanuka Geekiyanage Nov 27, 2026 869 views

Question details

Find nonblank cells efficiently with a VBA loop that processes each row from left to right.

Skip Blank Cells and Find the Next Nonblank Cell in Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A macro scans approximately 600 columns starting at column D across approximately 150 rows, beginning with row 2.
Observed behavior
Many cells are blank, and the loop needs to handle empty rows and trailing blanks without losing the required row-by-row order.
Before you start

Confirm the worksheet name and the last row and column to scan. Open the VBA editor with Alt+F11, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if it needs to retain the macro.

Solution 1Recommended

Use Find to process populated cells in row order

Search each bounded row from column D onward, stopping when Find returns no result or wraps back to an earlier column.

This example searches 600 columns starting at D and rows 2 through 150. Adjust the worksheet name and boundaries to match your data.

The search uses xlFormulas, so a cell containing a formula is included even if the formula displays an empty string. Truly empty cells are skipped.

1
Open a VBA module

In Excel, press Alt+F11 and select Insert > Module.

2
Add the row-by-row search

Paste the following macro into the module, then replace Sheet1 with your worksheet name: Sub ProcessNonblankCells() Dim ws As Worksheet Dim scanRange As Range, found As Range, afterCell As Range Dim r As Long, k As Long, previousCol As Long Const firstRow As Long = 2 Const lastRow As Long = 150 Const firstCol As Long = 4 Const columnCount As Long = 600 Set ws = ThisWorkbook.Worksheets("Sheet1") For r = firstRow To lastRow Set scanRange = ws.Range(ws.Cells(r, firstCol), _ ws.Cells(r, firstCol + columnCount - 1)) Set afterCell = scanRange.Cells(1, scanRange.Columns.Count) previousCol = firstCol - 1 Do Set found = scanRange.Find(What:="*", After:=afterCell, _ LookIn:=xlFormulas, LookAt:=xlPart, _ SearchOrder:=xlByColumns, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If found Is Nothing Then Exit Do If found.Column <= previousCol Then Exit Do k = found.Column Debug.Print found.Address, k, found.Value ' Add your processing code here. previousCol = k Set afterCell = found Loop Next r End Sub

3
Run and check the results

In the VBA editor, select the macro and press F5. Open View > Immediate Window to check the addresses and column numbers, then replace or extend the Debug.Print line with your processing code.

Use Find to process populated cells in row order
Get a column number from an address: If strAddress is a text address such as "$D$2", use k = ws.Range(strAddress).Column. If you already have a Range object such as found, use k = found.Column.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Excel spreadsheets. Its format compatibility helps you move existing files into your everyday workflow; check macro support before relying on Excel VBA code.

Open and edit Microsoft Excel XLS and XLSX filesNavigate and review large spreadsheet ranges in WPS SpreadsheetsUse a familiar interface for a smoother transition from Microsoft OfficeGet a free, lightweight office suite for everyday document work
microsoft office alternative - wps office

Frequently Asked Questions

Does the macro finish one row before moving to the next?

Yes. The outer For loop selects one row at a time, and Find searches from column D to the specified last column before the loop advances to the next row.

What happens if a row is empty or ends with blank cells?

If Find returns Nothing, the inner loop exits. If it wraps to a previously processed column after reaching the last populated cell, the column check exits the loop.

How do I get the column number from a cell address stored as text?

Use k = ws.Range(strAddress).Column, where ws is the worksheet containing the address. A text string does not have a Column property by itself.

Will the search include formulas that display a blank?

Yes. With LookIn:=xlFormulas, Find searches the stored formula, so a formula returning an empty string can be included.

How can I change the scan boundaries?

In the VBA module, change firstRow, lastRow, firstCol, or columnCount. Column D is column 4, and a columnCount of 600 scans 600 columns starting there.