Home » excel » excel – Count number of rows in worksheet

excel – Count number of rows in worksheet

Posted by: admin March 9, 2020 Leave a comment


I want to count number of rows in Sheet1, from the Sheet2 code module.

In the sheet1 code module, the following code works fine

ctr = Range("B2", Range("B2").End(xlDown)).Count

I tried the same code in the Sheet2 code module

recct = ThisWorkbook.Sheets("Sheet1").Range("B2", Range("B2").End(xlDown)).Count

I am getting run time error 1004 Application -Defined or Defined error


How to&Answers:

The error occurs in the 2nd range reference in recct. Because you are referencing a different sheet, you need to tell VBA the sheet name in both range references.

Try this instead:

With ThisWorkbook.Sheets("Sheet1")    
    recct = .Range("B2", .Range("B2").End(xlDown)).Rows.Count    
End With

Alternatively, this will work as well (though a bit sloppier).

recct = ThisWorkbook.Sheets("Sheet1").Range("B2", ThisWorkbook.Sheets("Sheet1").Range("B2").End(xlDown)).Rows.Count


Since there is a lot of discussion around what you actually mean by number of rows on the sheet, use the above code to literally start at B2 and count the number of contiguous cells directly underneath

However, if you want to find the last “real” used cell in column B (by real, I mean with data in it) do this:

With ThisWorkbook.Sheets("Sheet1")

    recct = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Rows.Count

End With


You can use this for example:

rowsInThere = Sheets("Sheet1").UsedRange.Rows.Count

This works without ranges. Also you might use ActiveSheet as a sheet to check, in case you would need to change current sheet and check its rows count.


Two things

  1. When working off sheet you need to fully qualify your range
  2. Always measure the last cell bottom up rather than top down – you may have gaps


Sub GetB()
Dim ws As Worksheet
Set ws = Sheets(1)
Dim lngCnt As Long
lngCnt = ws.Range(ws.[b2], ws.Cells(Rows.Count, "b").End(xlUp)).Count
End Sub

more robust

To handle all situations cleanly then Find is easier

Sub GetB()
    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = Sheets(1)
    Set rng1 = ws.Range("B:B").Find("*", ws.[b1], xlValues, , , xlPrevious)
    If Not rng1 Is Nothing Then
    Select Case rng1.Row
    Case 1
    MsgBox "Only B1 has data", vbCritical
    Case 2
    MsgBox "No used cells past B2"
    Case Else
    MsgBox rng1.Row - 1 & " cells between B2 and B" & rng1.Row
    End Select
        MsgBox ws.Name & " column B Is blank", vbCritical
    End If
End Sub


Don’t know if this will help but I use this in my modules all the time:

Dim TR as long, TC as long

TR = [Sheet1!A1].CurrentRegion.Rows.count
TC = [Sheet1!A1].CurrentRegion.Columns.count

If I know that if the dataset I’m dealing with doesn’t have an empty row or column, like an extract from another program or something, then it’s quick and works great!
From this I can specify a range select or perform a vlookup.

TR = [Sheet1!A1].CurrentRegion.Rows.count
[I2] = "=vlookup($C2,'sheet1'!A$2:B$" & TR & ",2,FALSE)"