VBA Row Count

What is Excel VBA Row Count?

In Excel VBA, the Row count refers to the number of rows within a worksheet or a specific range of cells. It is commonly used to determine the size or extent of a data set. To obtain the Excel VBA Row Count, you can use the Rows.Count property for VBA row count visible cells.

An example showcasing the row count of a sheet is shown below:

VBA Row Count Intro - Example

It counts all the rows available in “Sheet1” and prints the value in the Immediate tab, resulting in the output below:

VBA Row Count Intro - Output

You can modify this VBA code to suit your needs, such as obtaining the row count within a particular range or performing further operations based on the row count.

Key Takeaways
  • In Excel VBA Row Count, the Rows.Count returns the total number of rows in a worksheet. It represents the maximum row index that can be used in a worksheet.
  • The value returned by Rows.Count is based on the maximum number of rows allowed in the Excel version being used. In the most recent versions of Excel, this value is 1,048,576 rows.
  • The actual last used row may be less than the Rows.Count if there are empty rows below the last used row.
  • When using Rows.Count, consider the context in which it is used. It is commonly used in combination with other methods or properties to perform operations on specific ranges, such as finding the last used row in a column or looping through rows of data.

How to count rows in VBA?

Consider an example table consisting of multiple rows and columns where you need to find the number of rows in a specific range defined by the user using the VBA Row Count array.

VBA Row Count - Example

Step 1: Go to the “Developer tab” section in the toolbar and click the “Visual Basic” option. Now, the VBA Editor opens to add functions and Sub procedures. Then, click the “Insert” and “Module” buttons to create a new module or blank page.

VBA Row Count - Example - Step 1
VBA Row Count - Example - Step 1 - Module

Step 2: Define a sub-procedure to find the row count of a given range from cells A1-F10.

VBA Row Count - Example - Step 2

Step 3: Initialize three variables, RowCount to store the count of rows, ‘w’ to specify which sheet this sub-procedure will work on, and rng to define the range of cells to be worked on in this function.

VBA Row Count - Example - Step 3

Step 4: Set ‘w’ as the current worksheet to be working in, rng as the range of cells we’ll work on, and RowCount as the value returned by the Rows.Count function.

VBA Row Count - Example - Step 4

Step 5: Print the RowCount value using the Debug.Print function. It will print the output in the Immediate tab.

VBA Row Count - Example - Step 5

Code:

Sub RowCountforSpecificRange()
Dim RowCount As Long
Dim w As Worksheet
Dim rng As Range
Set w = ThisWorkbook.Worksheets(“Sheet1”)
Set rng = Range(“A1:F10”)
RowCount = rng.Rows.Count
Debug.Print “Row Count: ” & RowCount
End Sub

Step 6: Press the run arrow button or F5 to run the program. The output is shown below:

VBA Row Count - Example - Step 6

Here, we can see, irrespective of the number of columns in the table, Rows.Count returns only the number of rows.


Excel VBA – All in One Courses Bundle (35+ Hours of Video Tutorials)

If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

Examples

Let us look at some examples on how to use the Excel VBA Row count function in our examples.

Example #1

Assume an example where you want to implement a Boolean or gate for two inputs in an Excel Table. To do so, we need to perform a FOR loop that runs through the entire table, checking inputs in both columns “A” and “B” and printing the output of the Boolean ‘OR’ function in column “C.”

VBA Row Count - Example 1

Step 1: Create a sub-procedure that implements the Boolean OR gate function.

VBA Row Count - Example 1 - Step 1

Step 2: Then, define two variables lastRow and ‘,’’ where lastRow returns the row count of the table, and i is the iterative variable to be used in the For loop.

VBA Row Count - Example 1 - Step 2

Step 3: Set the row count variable as lastRow. For the table to be dynamic whenever there’s a change in row count, we use the Excel VBA row count xlUp function. The xlUp function returns the last active row in a select column with which we can find the count and the number of iterations needed to get the desired results.

VBA Row Count - Example 1 - Step 3

Step 4: Initialize a FOR loop from 0 to the total row count available in the table, then check if either value in columns A and B has the value 1, which will print the value 1 in column C, or else, it prints 0.

VBA Row Count - Example 1 - Step 4

Code:

Sub OrGate()
Dim lastRow As Long
Dim i As Long
lastRow = Range(“A” & Rows.Count).End(xlUp).Row
For i = 1 To lastRow
If Range(“A” & i).Value > 0 Or Range(“B” & i).Value > 0 Then
Range(“C” & i).Value = 1
Else: Range(“C” & i).Value = 0
End If
Next i
End Sub

Step 5: Run the module using F5. The output is shown below.

VBA Row Count - Example 1 - Step 5

Example #2

Consider an example where you need to copy a table from one place to another in the same sheet.

VBA Row Count - Example 2

To do this, we need to find the size of the table by returning the VBA row count array, using which we’ll copy the table from column A to column K in sheet 1.

Step 1: Create a sub-procedure that performs VBA copy-paste.

Example 2 - Step 1

Step 2: Define ‘ro,’ which will return the VBA row count visible cells. Then, define ‘rng’ as the range of the table, source as the table which will be copied, and destination as the part in the sheet where the table will be posted.

VBA Row Count - Example 2 - Step 2

Step 3: Assign ‘rng’ as the range of the given table and ‘ro,’ which returns the count of rows.

Example 2 - Step 3

Step 4: Set the source and destination cells. For the source, we assign the range of the table from columns A to F for the row count.

Example 2 - Step 4

Step 5: Perform the Excel VBA Copy-paste task using the source and destination.

VBA Row Count - Example 2 - Step 5

Code:

Sub CopyTables()
Dim ro As Long
Dim rng As Range
Dim source As Range
Dim destination As Range
Set rng = Range(“A1:F10”)
ro = rng.Rows.Count
Set source = Range(“A1:F” & ro)
Set destination = Range(“K1”)
source.Copy destination
End Sub

Step 6: Run the above code using F5 or by pressing the green triangle on the VBA toolbar. We will get the output shown below. The copied table will be pasted from cell K1 to cell P10:

VBA Row Count - Example 2 - Step 6

Example #3

In this example, we need to simulate a Boolean AND gate for three inputs and print ‘TRUE’ or ‘FALSE’ for the Excel VBA Row Count.

For this, we run a FOR loop through the table to check each value and apply the function depending on whether the conditions are true or false. To execute the FOR loop, we need the number of rows, which can be found by the Excel VBA Row Count xlDown function.

Example 3

Step 1: Initialize a sub-procedure which will implement the Boolean AND gate function.

Example 3 - Step 1

Step 2: Initialize two variables, ‘l,’ which will return the number of rows, and ‘c,’ an iterative variable for the FOR loop.

Example 3 - Step 2

Step 3: Find the number of rows using the Excel VBA Row Count xlDown function.

Example 3 - Step 3

Starting from cell A1 (Range(“A1”)), the End(xlDown) method is applied, which simulates pressing the ‘Ctrl + Down Arrow’ key combination in Excel. It causes Excel to move down from the starting cell until it reaches the last used cell in the column. Finally, the .Row property retrieves the row number of the last used cell.

Step 4: Initialize a for loop from cell 1 to the last used row in the table and check if the values are more significant than 0 in all columns using the Excel VBA AND function.

Example 3 - Step 4

If all values are greater than 0, it prints as “TRUE”; else, it prints as “FALSE.”

Code:

Sub AndGate()
Dim l As Long
Dim c As Long
l = Range(“A1”).End(xlDown).Row
For c = 1 To l
If Range(“A” & c).Value > 0 And Range(“B” & c).Value > 0 And Range(“C” & c).Value > 0 Then
Range(“D” & c).Value = True
Else: Range(“D” & c).Value = False
End If
Next c
End Sub

Step 5: Run the module using F5 or click the green triangle on the VBA toolbar. The output is as shown:

Example 3 - Step 5

Important Things To Note

  • Use the Rows.Count property to get the total number of rows in a worksheet or range. It provides a dynamic way to determine the row count, adapting to changes in the data.
  • Assign the row count to a variable for further use or store it in a specific location.
  • Verify if the row count is greater than zero before performing operations that depend on the existence of data.
  • Avoid hard-coding row counts unless you have a specific reason. Instead, use dynamic methods to determine the row count, allowing your code to adapt to changes in the data.
  • It’s important to handle cases where the worksheet has no data. In such cases, the Rows.Count will still return the maximum number of rows, but you may need to check if there is any actual data in the worksheet using methods like UsedRange or specific column checks.
  • Don’t assume that the last row with data is always the same as the row count. For example, it’s possible to have blank rows in between or data that extends beyond the last used row.
  • Avoid relying solely on the row count for data validation. Instead, always validate the data range using appropriate techniques, such as checking for empty cells or defined ranges.

Frequently Asked Questions (FAQs)

1. How do I count non-blank rows in Excel VBA?

To count non-blank rows in Excel VBA, you can use the SpecialCells method with the xlCellTypeConstants argument. This method allows you to select cells with constant values, excluding empty cells.

2. How to count Filter Rows in VBA?

To count filtered rows in Excel VBA, you can use the SUBTOTAL function in combination with the VisibleCells property. For example:

Set a Range object to the desired range of cells.
Apply a filter to the range using the AutoFilter method.
Use the SpecialCells(xlCellTypeVisible) property to select only the visible (filtered) cells.
Use the SUBTOTAL function with the COUNTA function as the first argument and the visible cells range as the second argument.
Retrieve the count using the VBA Value property.

3. What does cell row count 1 mean in VBA?

In VBA, Cells(row count, 1) refer to a specific cell within a worksheet.
An explanation of each component is written below:
Cells is a method used to reference cells within a worksheet.
Row count represents the row number of the cell you want to refer to.
1 indicates the column number of the cell you want to refer to. Here, one refers to the first column (column A).

Download Template

This article must be helpful to understand the VBA Row Count, with its formula and examples. You can download the template here to use it instantly.

This has been a guide to VBA Row Count. Here we learn how to count rows using Rows.count property in Excel VBA with examples and downloadable template. You may learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *