I am trying to put filter on the table which should return only one entry with goal to capture that entry row number. To achieve that I am trying to loop through range using .SpecialCells(xlCellTypeVisible)
property, however during my test I find out that loop still goes through all range, but always returns only visible cell row.
For example: I have table with 14 entries + heading row with filtered visible entry at 9th row. My code below returns 15 results, but rw.Row
always 9
. How to truly limit loop only to visible cells?
Sub Test()
Set ws = Sheets("Sheet1")
Set tbl = ws.ListObjects("Test_TBL")
With tbl
'sets filter criteria
.Range.AutoFilter Field:=1, Criteria1:="Specific Name"
.Range.AutoFilter Field:=2, Criteria1:="Specific Number"
'readjust range to emit headings row
With .AutoFilter.Range
Set AutoFilterRange = .Resize(.Rows.count - 1, .Columns.count).Offset(1, 0).SpecialCells(xlCellTypeVisible)
'in theory, only one (visible) record should be available for loop
For Each rw In AutoFilterRange
Debug.Print rw.Row
counter = counter + 1
Debug.Print counter
Next
End With
End With
End Sub
For Each rw In AutoFilterRange
loops through all cells of AutoFilterRange
because it defaults to AutoFilterRange.Cells
and not through all rows which would mean AutoFilterRange.Rows
.
Further AutoFilterRange
can be a discontinous range that can consist of multiple areas. So you need to loop through all areas and through all rows in each area, to get all rows in a discontinous range.
Dim Ar As Range
For Each Ar In AutoFilterRange.Areas ' loop through areas
Dim Rw As Range
For Each Rw In Ar.Rows ' loop through rows in each area
Debug.Print Rw.Row ' return row number
Next Rw
Next Ar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.