How to Count Filtered Rows in Excel with VBA (Step-by-Step Guide)

Suppose you have the following dataset.


Step 1 – Apply a Filter to the Dataset

  • Select any cell of your dataset.

Apply Filter in the Dataset

  • Go to the Data tab and select Filter.

Apply Filter in the Dataset

  • Each column can now be filtered.

Apply Filter in the Dataset

Read More: Excel VBA to Count Rows with Data


Step 2 – Launch the VBA Window to Count Filtered Rows in Excel

  • Go to the Developer tab and select Visual Basic. It will open the Visual Basic window.

Launch the VBA Window to Count Filtered Rows in Excel

  • In the Visual Basic window, select the Insert Menu and select Module from the drop-down menu.

Launch the VBA Window to Count Filtered Rows in Excel

  • The Module window will appear.

Launch the VBA Window to Count Filtered Rows in Excel


Step 3 – Type and Save the Excel VBA Code

  • Type this code in the Module window:
Sub Count_Filtered_Rows()
Dim wks As Worksheet
Dim zCount As Long, y As Long
Dim xRng As range
Set wks = ThisWorkbook.Worksheets(2)
zCount = 0
For y = 1 To wks.range("B4").CurrentRegion.Rows.Count
If wks.Cells(y, 1).EntireRow.Hidden = False Then
zCount = zCount + 1
End If
Next y
MsgBox "There are " & zCount & " rows"
End Sub

Type and Save the Excel VBA Code

Note: Fill in the working sheet number between the parentheses in this line: ThisWorkbook.Worksheets(). In this example, that’s Sheet 2. Also fill in the cell where your dataset starts in the parentheses here: wks.range().CurrentRegion.Rows.Count“B4” in this example.

  • Press Ctrl + S to save the code or click the Save icon.
  • Close the Visual Basic window.

Step 4: Run the VBA Code to Count Filtered Rows in Excel

  • To run the saved code, go to the Developer tab and select Macros.

Run the VBA Code to Count Filtered Rows in Excel

  • Select the code and click Run from the Macro window.

Run the VBA Code to Count Filtered Rows in Excel


Final Output after Running Excel VBA Code to Count Filtered Rows

  • The number of filtered rows (including the header) will show in a pop-up message box.

Final Output after Running Excel VBA Code to Count Filtered Rows

Example

  • Apply a filter to the dataset.

Final Output after Running Excel VBA Code to Count Filtered Rows

  • Go to the Developer tab and select Macros.

  • Run the code from the Macro window.

  • The results have changed.

  • To display the result without the header row, use this code:
Sub Count_Filtered_Rows()
Dim wks As Worksheet
Dim zCount As Long, y As Long
Dim xRng As range
Set wks = ThisWorkbook.Worksheets(2)
zCount = -1
For y = 1 To wks.range("B4").CurrentRegion.Rows.Count
If wks.Cells(y, 1).EntireRow.Hidden = False Then
zCount = zCount + 1
End If
Next y
MsgBox "There are " & zCount & " rows"
End Sub

Use zCount = -1 instead of zCount = 0.

  • Press Ctrl + S to save the code.
  • Go to the Developer tab and select Macros to Run the code.

Read More: Excel VBA: Count Rows with Specific Data


Things to Remember

  • Apply filters before using VBA.
  • Use zCount = -1 instead of zCount = 0 to count the row numbers without the header.
  • You can also run the saved code by pressing the F5 key.

Download Practice Book

Download the practice book.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mursalin Ibne Salehin
Mursalin Ibne Salehin

Mursalin Ibne Salehin holds a BSc in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. Over the past 2 years, he has actively contributed to the ExcelDemy project, where he authored over 150 articles. He has also led a team with content development works. Currently, he is working as a Reviewer in the ExcelDemy Project. He likes using and learning about Microsoft Office, especially Excel. He is interested in data analysis with Excel, machine learning,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo