0

I used

for i= 2 to activesheet.usedrange.rows.count

but the moment i am filtering columns on certain criteria and later put some If conditions I am not able to define the same way for visible rows.

Kindly help

Community
  • 1
  • 1
Pradip
  • 1
  • 1
    possible duplicate of [Row count on the Filtered data](http://stackoverflow.com/questions/17285897/row-count-on-the-filtered-data) – silentsurfer Jan 07 '15 at 10:07

1 Answers1

0

Here is a function I created to count the visible rows in a filtered list, and it also works with non-filtered ranges where rows are hidden. You have to be careful of working with filtered ranges. (If rows are contiguous in the source data then they could be contiguous in the filtered data. So three rows could = 1 area.) So to get around that I am looping through each area and then counting the rows in each area.

Function CountVisibleRowsInFilteredAreas(Optional myRange As Range)
Dim lCount As Long
Dim lCount2 As Long
Dim CurrRng As Range
Dim vArrRows As Variant
Dim vArrUnqRows As Variant
Dim iRow As Long
Dim iUnq As Long
Dim nUnq As Long
Dim rw As Range
Dim isUnq As Boolean

'if range to use was not specified in the code, then use the ActiveSheets' UsedRange
If myRange Is Nothing Then Set myRange = ActiveSheet.UsedRange

'this is created for autofiltered ranges, or non-autofiltered ranges with hidden rows,
'to count the number of rows that are visible.
'it assumes there is a header row and will count that as a row

'count the number of rows in each area to get the upper bound for the array that
 'will contain the row numbers of all of the rows in each area.
 lCount = 0
    For Each CurrRng In myRange.SpecialCells(xlCellTypeVisible).Areas
        lCount = lCount + CurrRng.Rows.Count
    Next CurrRng

'dim the array and give it upper and lower bounds, Set up a second counter to identify
'which row the loop is on, then loop through each row in each area,
'and get the row number and store it in an array.
ReDim vArrRows(1 To lCount)
    lCount2 = 0
    For Each CurrRng In myRange.SpecialCells(xlCellTypeVisible).Areas
        For Each rw In CurrRng.Rows
            lCount2 = lCount2 + 1
            vArrRows(lCount2) = rw.Row
        Next rw
    Next CurrRng

'remove duplicates/count unique rows
ReDim vArrUnqRows(1 To lCount2)
nUnq = 0
For iRow = 1 To lCount2
    isUnq = True
    'first one in vArrRows is always unique, and added to vArrUnqRows, the rest are compared
    'against the known unique ones in vArrUnqRows, if they are unique then they are added
    'to vArrUnqRows
    For iUnq = 1 To nUnq
        If vArrRows(iRow) = vArrUnqRows(iUnq) Then
            isUnq = False
            Exit For
        End If
    Next iUnq
    'add unique row numbers to vArrUnqRows
    If isUnq = True Then
        nUnq = nUnq + 1
        vArrUnqRows(iUnq) = vArrRows(iRow)
    End If
Next iRow
ReDim Preserve vArrUnqRows(1 To nUnq)
'creates an array of the row numbers of visible rows, but that is not used here and only the
'count of the visible rows is returned (nUnq)
CountVisibleRowsInFilteredAreas = nUnq

End Function
daria8877
  • 11
  • 1
  • 3