Excel VBA Visible Range: Step-by-Step Guide
In this article
To work with only the visible cells in Excel VBA, use Range.SpecialCells(xlCellTypeVisible) or loop through Areas of the filtered range. This matters because a normal range reference includes hidden rows and columns, so copying, deleting, or formatting it will also affect data you cannot see. The steps below show the safe pattern for reading, copying, and acting on visible cells only.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- A worksheet with a filter or manually hidden rows so there is something to test against.
- Basic familiarity with the VBA Editor (Alt+F11) and the Immediate Window (Ctrl+G).
Step-by-Step
- Open the VBA Editor and insert a module. Press Alt+F11, then in the Project Explorer right-click your workbook and choose Insert > Module. Menu labels can differ slightly by Excel version and region, but the Insert menu is consistent.
- Declare your range and a variable for the visible portion. For example:
Start with a fixed range while testing; swap to a dynamic range once the logic works.Dim rng As Range Dim vis As Range Set rng = ActiveSheet.Range("A1:D100") - Get the visible cells. Use:
This returns only cells that are not hidden by a filter or by a hidden row/column. If every cell is visible, it returns the whole range; if none are visible, it raises an error, so wrap it in error handling (see Troubleshooting).Set vis = rng.SpecialCells(xlCellTypeVisible) - Check the result in the Immediate Window. Type
? vis.Addressand press Enter. You will see a multi-area address like$A$1:$D$1,$A$5:$D$7. Each comma-separated block is anArea. This is the key concept: a visible range is often not one rectangle. - Loop through the areas when you need to act on each block. For example, to write a value into column E of every visible row:
Do not assumeDim ar As Range For Each ar In vis.Areas ar.EntireRow.Columns("E").Value = "Checked" Next arvis.Rows.Countequals the number of visible rows; it counts rows in the underlying range, not the visible ones. - Copy visible cells only. Use
vis.Copy Destination:=Sheets("Sheet2").Range("A1"). Copyingrnginstead would carry hidden rows across. If you paste manually, Excel pastes into contiguous rows, so the visible rows land next to each other without gaps. - Count visible rows correctly. Use
Application.WorksheetFunction.Subtotal(103, rng.Columns(1)). The 103 argument counts visible non-blank cells and ignores hidden rows, which is the same logic as the SUBTOTAL function in a cell. - Delete or clear only visible rows. Loop the areas in reverse order so deleting one block does not shift the addresses of the next:
Test on a copy of the workbook first; deletion cannot be undone by VBA.For i = vis.Areas.Count To 1 Step -1 vis.Areas(i).EntireRow.Delete Next i - Handle the "no visible cells" case. Add
On Error Resume Nextbefore theSpecialCellsline, then checkIf vis Is Nothing Thenand exit the sub with a message. Turn error handling back off withOn Error GoTo 0. - Run and verify. Press F5, then compare the Immediate Window output and the destination sheet against what you see on screen. If they match, replace the fixed range with something dynamic such as
ActiveSheet.UsedRangeor a named range.
Common Mistakes
- Treating the visible range as one rectangle. It is a collection of areas. Using
vis.Value = ...on a multi-area range can fail or write to the wrong cells. - Using
Rows.Counton the visible range. That returns the count for the whole underlying range, not the visible rows. Use SUBTOTAL with 103. - Forgetting that hidden columns behave the same way.
SpecialCells(xlCellTypeVisible)excludes hidden columns too, so a visible range can be split vertically as well as horizontally. - Deleting areas in forward order. Row numbers shift after each delete, so later areas point at the wrong rows. Always loop backwards.
- Assuming filters are the only cause. Manually hidden rows, grouped outline levels, and hidden columns all affect the result.
Tips
- Store the visible range in a variable once and reuse it; recalculating
SpecialCellsrepeatedly is slow on large sheets. - Use
vis.Areas.Countto see how fragmented the range is before choosing a loop strategy. - When writing results back, write to a helper column and paste values afterwards if you need to preserve formatting.
- Turn off
ScreenUpdatingaround long loops and turn it back on in the cleanup line, even if an error occurs. - Keep a test workbook with a filter, a hidden row, and a hidden column so you can confirm all three cases behave as expected.
Troubleshooting
Run-time error 1004: "No cells were found." This means every cell in the range is hidden or the range is empty. Wrap the call in error handling and exit gracefully rather than letting the macro stop.
The macro copies hidden rows anyway. Check that you are copying the vis variable, not the original rng. Also confirm no other code re-selects the full range before the copy.
Only some visible rows are processed. You are probably looping vis.Rows instead of vis.Areas. Switch to the areas loop shown in step 5.
Deletion removes the wrong rows. Loop the areas backwards and verify the range boundaries in the Immediate Window before deleting. Work on a copy of the file until the logic is confirmed.
Results differ from what is on screen. Refresh the filter or recalculate before running the macro; a stale filter state can leave SpecialCells returning an unexpected set.
Frequently Asked Questions
Does SpecialCells(xlCellTypeVisible) include manually hidden rows?
Yes. It excludes any row or column hidden by a filter, by manual hiding, or by grouping, so the result reflects exactly what is visible on screen.
Why does my visible range have multiple areas?
Because hidden rows or columns split the range into separate blocks. Each contiguous block of visible cells becomes its own Area, and you must loop them to act on all of them.
How do I count visible rows in VBA?
Use Application.WorksheetFunction.Subtotal(103, rng.Columns(1)). The 103 function number counts visible non-blank cells and ignores hidden rows.
Can I paste visible cells into another sheet without gaps?
Yes. Copy the visible range and paste to a single destination cell; Excel places the visible rows contiguously, skipping the hidden ones.
What happens if no cells are visible?
SpecialCells raises run-time error 1004. Handle it with On Error Resume Next, check whether the variable is Nothing, and exit the procedure with a message.
Does this work on protected sheets?
Only if the protection allows the action. Reading visible cells usually works, but deleting rows or writing values requires the sheet to be unprotected or the relevant permissions enabled.