wsnyder
Board Regular
- Joined
- Sep 23, 2018
- Messages
- 224
- Office Version
- 365
- Platform
- Windows
Hi all,
Using Excel 365.
How do I get all visible rows count after autofilter so I can loop through visible values to pass to another sub.?
I tired to get a new Range of visible rows which appears to give the correct non-contiguous address, but not the correct count of visible rows
Thanks,
-w
Using Excel 365.
How do I get all visible rows count after autofilter so I can loop through visible values to pass to another sub.?
I tired to get a new Range of visible rows which appears to give the correct non-contiguous address, but not the correct count of visible rows
Thanks,
-w
VBA Code:
Option Explicit
Sub foo()
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim xrng As Range
Dim rngHeader As Range
Dim i As Long 'column for filter
Dim j As Long
Const word_phrase As String = "Include"
Const crit As String = "N"
Set wb = ThisWorkbook
Set ws = wb.Worksheets(1)
Set rng = ws.Range("A1").CurrentRegion
Set rngHeader = ws.Rows("1:1")
i = FindColumnHeader(rng:=rngHeader, _
SearchTerm:=word_phrase)
Debug.Print "i: "; i
rng.AutoFilter field:=i, _
Criteria1:="<>" & crit
With rng
Set xrng = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
End With
Debug.Print "xrng address: "; xrng.Address
Debug.Print "xrng rows count: "; xrng.Rows.Count
Debug.Print "==================================="
For j = 1 To xrng.Rows.Count
Debug.Print j, xrng.Cells(j, 1).Value, xrng.Cells(j, 3).Value
Next j
Set rng = Nothing
Set xrng = Nothing
Set ws = Nothing
Set wb = Nothing
End Sub