Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 More…Less
If you need a quick way to count rows that contain data, select all the cells in the first column of that data (it may not be column A). Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count.
Do the same thing to count columns, but this time click the row selector at the left end of the row.
The status bar then displays a count, something like this:
If you select an entire row or column, Excel counts just the cells that contain data. If you select a block of cells, it counts the number of cells you selected. If the row or column you select contains only one cell with data, the status bar stays blank.
Notes:
-
If you need to count the characters in cells, see Count characters in cells.
-
If you want to know how many cells have data, see Use COUNTA to count cells that aren’t blank.
-
You can control the messages that appear in the status bar by right-clicking the status bar and clicking the item you want to see or remove. For more information, see Excel status bar options.
Need more help?
You can always ask an expert in the Excel Tech Community or get support in the Answers community.
Need more help?
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
Thanks
asked Oct 26, 2012 at 13:42
2
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
Update
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
answered Oct 26, 2012 at 13:49
Scott HoltzmanScott Holtzman
27k5 gold badges36 silver badges72 bronze badges
8
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.
brettdj
54.6k16 gold badges113 silver badges176 bronze badges
answered Oct 26, 2012 at 13:48
VesperVesper
18.5k6 gold badges37 silver badges60 bronze badges
5
Two things
- When working off sheet you need to fully qualify your range
- Always measure the last cell bottom up rather than top down — you may have gaps
code
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
Else
MsgBox ws.Name & " column B Is blank", vbCritical
End If
End Sub
answered Oct 26, 2012 at 13:56
brettdjbrettdj
54.6k16 gold badges113 silver badges176 bronze badges
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)"
answered Jun 3, 2014 at 17:02
-
#2
Do not understand «number of rows».
Do you mean number of filled rows?
And what column are we looking in for number of filled rows?
-
#3
Maybe he means how to link the sheet name in column A into the formula in column B without having to type it in. Otherwise, it’s just a simple formula (based on the column that has the data for counting)
Code:
=COUNTA(Sheet1!A1:A1048576)
-
#5
Try this:
Code:
Option Explicit
Sub ShtRow_Counting()
Dim oWbk As Workbook
Dim oSht As Worksheet
Dim x As Integer
Set oWbk = ActiveWorkbook
Worksheets.Add After:=Sheets(Worksheets.Count)
ActiveSheet.Name = "Row Counts"
Range("A1").Value = "SHeet name"
Range("B1").Value = "Count of records"
x = 2
For Each oSht In oWbk.Worksheets
Range("A" & x).Value = oSht.Name
Range("B" & x).Formula = "=COUNTA('" & oSht.Name & "'!A1:A" & Rows.Count & ")"
x = x + 1
Next oSht
MsgBox "Done."
End Sub