QP School

Full Version: Count the number of rows in a worksheet with VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub CountRows()
    Dim rowCount As Long

    rowCount = ActiveSheet.UsedRange.Rows.Count

    MsgBox "The number of rows in the worksheet is: " & rowCount & "."
End Sub

Explanation:

This code defines a subroutine named CountRows. 
It declares a variable rowCount to hold the count of rows. 
The UsedRange property is used to get the range of cells that have data in the active worksheet. 
The Rows.Count property gets the number of rows in that range. 
The count of rows is then displayed in a message box.

The ActiveSheet object represents the currently active worksheet. 
The UsedRange property returns a Range object that represents the used range on the worksheet. 
The Rows.Count property returns the number of rows in that range.