Here is another way to write it...
Sub CountVisibleRows()
Dim R As Range, C As Long
With Worksheets("Sheet2").AutoFilter.Range
C = Range(.Item(2), .Item(.Count)).SpecialCells(xlVisible).Count
End With
MsgBox "There are " & C & " visible AutoFilter'ed rows."
End Sub
Using the Item(2) makes the count skip the header row; if you want to count
the header row, then change it to Item(1).
--
Rick (MVP - Excel)
"OssieMac" <(E-Mail Removed)> wrote in message
news:5E7C120A-31E7-4CA2-AB8F-(E-Mail Removed)...
> Hello Paul and Gary's Student,
>
> With filtered data and SpecialCells(xlVisible) you cannot count rows
> because
> the Rows.Count only returns a number up to the end of the first visible
> block.
>
> However, you can count the cells. Therefore set a range to one column of
> the
> visible data and count the cells in that range as follows.
>
> Sub CountVisibleRows()
> Dim rngFiltColumn As Range
>
> 'Edit "Sheet1" to your sheet name
> With Sheets("Sheet1").AutoFilter.Range
> Set rngFiltColumn = .Offset(1, 0) _
> .Resize(.Rows.Count - 1, 1) _
> .SpecialCells(xlVisible)
> End With
>
> MsgBox rngFiltColumn.Cells.Count
>
> End Sub
>
> An explanation of the setting of rngFiltColumn.
> Offset moves it down one row off the column headers. However, this then
> includes an additional row at the bottom so Resize reduces by one row and
> the
> column parameter in Resize set to 1 only returns one column.
> SpecialCells(xlVisible) is self explanatory.
>
> As some added info. You cannot use for i = 1 to Rows.count with the
> filtered
> data. However, you can use for each cell in the column range as follows.
>
> Dim c As Range
> Dim i As Long
> For Each c In rngFiltColumn
> 'Reference adjacent columns with offset.
> For i = 0 To 5
> MsgBox c.Offset(0, i).Value
> Next i
> Next c
>
> Note for ease of programming you can use Offset(0, 0) which effectively
> does
> not offset. This is used in the first iteration of the above.
>
> Hope this helps.
>
> --
> Regards,
>
> OssieMac
>
>
>
|