How To Count Rows In Excel Vba

To count rows in Excel VBA, you can use the `Rows.Count` property. Here’s how you can do it:

“`vba
Dim rowCount As Long
rowCount = Sheets(“Sheet1”).UsedRange.Rows.Count
“`

Let’s break down the code:

1. We declare a variable `rowCount` as a `Long` data type to store the number of rows.
2. `Sheets(“Sheet1”)` refers to the specific worksheet where you want to count the rows. Replace “Sheet1” with the actual name of your sheet.
3. `UsedRange` returns the range of used cells in the worksheet.
4. `Rows.Count` gives you the number of rows in the range.

Here’s an example to demonstrate how it works:

Let’s say we have a worksheet with data in cells A1 to A10. To count the number of rows, we can use the following code:

“`vba
Dim rowCount As Long
rowCount = Sheets(“Sheet1”).Range(“A1:A10”).Rows.Count
MsgBox “Total Rows: ” & rowCount
“`

This code will display a message box with the total number of rows, which in this case would be 10.

By using the `Rows.Count` property along with the appropriate range, you can easily count the number of rows in Excel VBA.

Similar post

Leave a comment