Suppose I have numbers 1 to 10 in cells
A2:A11 with my autofilter in
A1. I now filter to only show numbers greater then 5 (i.e. 6, 7, 8, 9, 10).
This code will only print visible cells:
Sub SpecialLoop() Dim cl As Range, rng As Range Set rng = Range("A2:A11") For Each cl In rng If cl.EntireRow.Hidden = False Then //Use Hidden property to check if filtered or not Debug.Print cl End If Next End Sub
Perhaps there is a better way with
SpecialCells but the above worked for me in Excel 2003.
Just found a better way with
Sub SpecialLoop() Dim cl As Range, rng As Range Set rng = Range("A2:A11") For Each cl In rng.SpecialCells(xlCellTypeVisible) Debug.Print cl Next cl End Sub
~ Answered on 2012-06-01 12:09:51