We can count rows with data by using workbook functions or conditional formatting but if we work with a large amount of data then using VBA (Visual Basic for Applications) is much more effective and time-saving. So this article will help you with 4 VBA Macros to count rows with data in Excel including clear steps and vivid illustrations.
Download Practice Workbook
You can download the free Excel template from here and practice on your own.
4 VBA Codes to Count Rows with Data in Excel
For applying VBA Macros I have made a dataset that contains some students’ names, IDs, and ages.
1. Count Rows with Data Using Excel VBA in a Whole Sheet
In this first example, I’ll show how to count used rows in a whole worksheet using VBA codes which means it will count all the rows with any data anywhere in the worksheet.
Steps:
- Right-click on the sheet title.
- Select View Code from the context menu.
Soon after a VBA window will open up.
- Then type the codes given below-
Sub count_rows_with_data_wholeSheet()
Dim x As Long
Dim y As Range
With ActiveSheet.UsedRange
   For Each y In .Rows
       If Application.CountA(y) > 0 Then
           x = x + 1
       End If
   Next
End With
MsgBox "Number of rows with data is " & x
End Sub
- Later, press the Play icon to run the codes.
Then you will get the output with a pop-up message box.
Read More: How to Count Rows with VBA in Excel (5 Approaches)
2. Embed VBA Code to Count Rows with Data for a Range Contains Continuous Data
Now I’ll show how to count rows with data if you want to count them from a selection of continuous data. It’s very helpful for some particular moments.
Steps:
- Select your continuous data range.
- Right-click on your sheet title.
- Select View Code from the context menu.
- After the VBA window appears, write the following codes in it-
Sub CountUsedRows()
Dim x As Long
x = Selection.Rows.Count
MsgBox x & " rows with data in the selection"
End Sub
- Finally, just press the Play icon to run the codes.
Now see that a message box is showing the counted rows.
3. Determine Used Rows Using Excel VBA for One Column
If we need to calculate rows with data for a specific column then it is also possible to do using Excel VBA. I have removed a row from the dataset. Now I’ll use VBA to count the used rows in Column B.
Steps:
- Select data range from the specific column.
- Right-click on that sheet title.
- Select View Code from the context menu.
A VBA window for that sheet will open up.
- Type the following codes-
Sub CountRows_with_Data_in_a_Column()
Dim Total_Rows As Long
Dim Blank_Rows As Long
Set Rng = Selection
Total_Rows = Rng.Rows.Count
Blank_Rows = Application.WorksheetFunction.CountBlank(Rng)
MsgBox "The Number of Used Rows is: " + Str(Total_Rows - Blank_Rows)
End Sub
- Click the Play icon for running the codes.
A dialog box named ‘Macros’ will appear.
- Select the Macro name which we used in the codes.
- Then press Run.
Soon after you will get the output like this in a message box.
4. Run Excel VBA Code to Calculate Used Rows with Specific Word
In our last example, I’ll explain the procedure to count used rows with a specific word that means it will count only those rows which contain our selected specific word. Here I’ll use VBA to count the rows which contain the specific word ‘15’, Let’s go forward.
Steps:
- Select data range from the column where the specific word exists.
- Then Right-click on that sheet title.
- Click View Code from the context menu.
- After opening the VBA window type the following codes-
Sub Count_Rows_with_SpecificWord()
Dim counter As Long
counter = 0
Dim Rng As Range
Set Rng = Selection
For i = 1 To Rng.Rows.Count
       If Rng.Cells(i, 1).Value = 15 Then
           counter = counter + 1
       End If
Next i
MsgBox "Number of rows with specific word: " + Str(counter)
End Sub
- Now to run the codes, press the Play icon.
- After that select the Macro name which is specified in the codes.
- Later, just press Run.
A pop-up message box will then show the result.
Read More: How to Count Rows with Text in Excel (Easiest 8 Ways)
Conclusion
I hope all of the methods described above will be good enough to count rows with data using VBA in Excel. Feel free to ask any question in the comment section and please give me feedback.