Excel VBA Offset to Next Visible Cell
In this article
When working with filtered data in Excel, using VBA to navigate to the next visible cell avoids errors caused by hidden rows. The Offset method alone skips over filtered-out cells, so you need to combine it with the SpecialCells method to find only visible cells. This technique ensures your macro moves accurately through visible data after filtering, without jumping over rows or crashing on empty hidden cells.
What You'll Need
- Microsoft Excel with VBA enabled (Excel 2010 or later)
- A worksheet with filtered data (using Data > Filter)
- Basic familiarity with the VBA Editor (Alt+F11)
- At least one column with visible and hidden rows after filtering
Step-by-Step
- Open your Excel workbook and ensure your data has been filtered using the Filter button (Data tab > Filter). At least one row must be hidden due to the filter.
- Press Alt+F11 to open the VBA Editor. In the Project Explorer, double-click the worksheet where your filtered data resides (e.g., Sheet1).
- Insert a new module by clicking Insert > Module, or paste the code directly into the worksheet’s code window if you prefer to bind it to that sheet.
- Copy and paste the following VBA function into the editor:
Function NextVisibleCell(startCell As Range) As Range
Dim nextCell As Range
Set nextCell = startCell.Offset(1, 0)
Do While nextCell.Hidden = True Or IsEmpty(nextCell.Value) And nextCell.Row <= startCell.Worksheet.Cells(startCell.Worksheet.Rows.Count, startCell.Column).End(xlUp).Row
Set nextCell = nextCell.Offset(1, 0)
If nextCell.Row > startCell.Worksheet.Cells(startCell.Worksheet.Rows.Count, startCell.Column).End(xlUp).Row Then Exit Do
Loop
If nextCell.Row > startCell.Worksheet.Cells(startCell.Worksheet.Rows.Count, startCell.Column).End(xlUp).Row Then
Set NextVisibleCell = Nothing
Else
Set NextVisibleCell = nextCell
End If
End Function
- Alternatively, use this more robust method that leverages SpecialCells to find visible cells directly:
Function NextVisibleCellRobust(startCell As Range) As Range
Dim visibleRange As Range
Dim foundCell As Range
On Error Resume Next
Set visibleRange = startCell.Parent.UsedRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If visibleRange Is Nothing Then Exit Function
Dim cell As Range
For Each cell In visibleRange
If cell.Row > startCell.Row And cell.Column = startCell.Column Then
Set NextVisibleCellRobust = cell
Exit Function
End If
Next cell
End Function
- Save the workbook as a macro-enabled file (.xlsm). If you don't, VBA code will be lost when you close.
- Return to Excel and select a cell in your filtered column that is not the last visible row (e.g., cell A5 if rows A6 and A7 are hidden).
- Press Alt+F8 to open the Macro dialog, then run the macro you created. Or, call the function from another subroutine:
Sub TestNextVisible()
Dim currentCell As Range
Dim nextVis As Range
Set currentCell = ActiveCell
Set nextVis = NextVisibleCellRobust(currentCell)
If Not nextVis Is Nothing Then
nextVis.Select
MsgBox "Next visible cell is: " & nextVis.Address
Else
MsgBox "No more visible cells below."
End If
End Sub
- Test the macro by filtering your data to hide several rows, selecting a visible cell, and running TestNextVisible. The selection should jump to the next visible cell below, skipping all hidden rows.
Common Mistakes
- Using
Offset(1,0)alone without checking visibility — this will select hidden rows and cause logic errors in downstream code. - Assuming all data is in a contiguous range — if your data has gaps or empty rows, the
xlUpmethod may stop too early. Always validate the full range usingUsedRangeor define explicit boundaries. - Not handling the case where no visible cell exists below — this causes a runtime error if you try to select or reference a Nothing object. Always use
If Not nextVis Is Nothing Then. - Running the code on an unfiltered sheet — the function will still work, but it may behave unexpectedly if there are no hidden rows. Add a filter check if needed.
- Using
Selectioninstead of explicitly defining the starting cell — this makes the macro fragile and dependent on user selection. Always pass a Range object explicitly.
Tips
- Use
NextVisibleCellRobustover the basic loop version — it's faster on large datasets because it uses Excel’s built-in visible cell enumeration instead of iterating row by row. - For better performance, limit your search to the column of interest by defining the range as
Range("A:A")instead ofUsedRangeif you know the column. - Store the result of
NextVisibleCellRobustin a variable and reuse it instead of calling the function multiple times in loops. - Combine this with
AutoFiltertoggling to automate filtering and navigation in sequence — ideal for reporting or data extraction macros. - Test your macro with different filter combinations: single-column filters, multi-column filters, and custom filters to ensure robustness.
- Use
Debug.Printstatements to trace the cell addresses being evaluated during development.
Troubleshooting
- Macro doesn't run or gives a "Compile Error" — Check that you're not using
Functioninside a regular module without declaring it as Public. Functions called from other subroutines must be Public by default, but if you placed the function inside a worksheet module, ensure you're calling it from the correct context. - It skips to the bottom of the sheet — You likely have a blank cell in the column that Excel interprets as the end of data. Use
Cells(Rows.Count, col).End(xlUp)to find the true last row, not just the first blank. Ensure your data has no accidental blank rows in the middle. - "Object variable or With block variable not set" — This means the function returned Nothing and you tried to use it without checking. Wrap all usage in an
If Not IsNothing(...)block. - Code works in debug mode but not when run — This often happens if the worksheet is protected. Unprotect it before running the macro, or add
ActiveSheet.UnprotectandActiveSheet.Protectaround your code if needed. - NextVisibleCellRobust returns the wrong cell — Make sure the starting cell is visible. If the starting cell is hidden, the function starts looking from the first visible cell after it — which might not be intuitive. Always ensure the starting cell is visible before calling.
- Macro slows down dramatically on large tables — Switch to the SpecialCells method (NextVisibleCellRobust). Avoid looping through hundreds of rows manually. SpecialCells is optimized by Excel’s engine.
Frequently Asked Questions
Can I use this to go to the next visible cell in a different column?
Yes, but you must adjust the column offset. ChangeOffset(1, 0) to Offset(1, 1) to move right one column, or use cell.Column + offset in the loop condition to search in a different column.
Does this work with manually hidden rows (not filtered)?
No. SpecialCells(xlCellTypeVisible) only detects rows hidden by AutoFilter, not by right-click > Hide. For manually hidden rows, use a different method, like checkingRows(rowNumber).Hidden in a loop.
Why does my macro sometimes jump to the top of the sheet?
This happens if the starting cell is the last visible cell in the column. The function returns Nothing, and if your code tries to select Nothing, Excel may default to the first cell. Always check if the result is Nothing before selecting.Can I use this with table structures (ListObjects)?
Yes, but reference the table’s data body range:MyTable.DataBodyRange. Then apply the same logic to that range instead of UsedRange. Table structures are more stable and avoid issues with inserted rows.
Will this work in Excel Online or mobile?
No. VBA is not supported in Excel Online, Excel for iPad, or Excel for Android. This method only works in desktop versions of Excel for Windows or Mac.What if I need to go upward to the previous visible cell?
Modify the function to useOffset(-1, 0) and loop downward from the last row upward. Use Cells(1, col) as the bottom boundary and check cell.Row < startCell.Row instead.