Counting visible rows

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a filter that is run on my sheet. The data starts in A4 and I am trying to write the code that will determine if there is more than 1 result for the autofiler, ie. there are more visible rows eg. row 4, row 5 etc

So far I have this code but this code counts the actual cells, not just the visible ones.

VBA Code:
                                Dim lRow As Long
                                lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
                                If lRow > 4 Then


Can someone tell me the code to count the visible rows please?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Your code will find the last row. So, it is still valid if you want to loop through 1st to last row

If you want to count how many visible row, you can

VBA Code:
Dim Rng as range, xCell as range

For Each xCell In Rng
    If Not xCell.EntireColumn.Hidden Then
       xCount = xCount + 1
    End If
Next

I don't know if later Excel version has simple command. I'm using 2016. You should update your profile.
 
Upvote 0
VBA Code:
Sub v()
Dim rws&: rws = Range("A4:A" & Cells(Rows.Count, 1).End(3).Row).SpecialCells(xlCellTypeVisible).Count
End Sub
 
Upvote 0
Solution
Hi, Use below:

VBA Code:
Sub visibleRow()
    Dim lRow As Long, visibleRow As Long, RowCount As Long
    lRow = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, 1).End(xlUp).Row
    visibleRow = 0
    
    For RowCount = 5 To lRow
        If Not (Sheets("Sheet1").Range("A" & RowCount).EntireRow.Hidden) Then visibleRow = visibleRow + 1
    Next
    
    If visibleRow > 4 Then MsgBox "Yes"
End Sub
 
Upvote 0
Here is a better way I think

VBA Code:
Sub VisibleRow()

Dim j As Long
Dim rng As Range
Dim ws As Worksheet

Set ws = Sheets("Sheet1")
Set rng = ws.Range("A1:A10").SpecialCells(xlCellTypeVisible)
j = rng.Count

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top