Number Of Rows with Data

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Afternoon

Using the below to return the Number of Rows in Column B. The Column currently does not have any data, when the code is run the result is 1. Not sure why this is.

Sub CountRowsColumn()

MsgBox "Number of Rows with data in Column b is" & " " & Cells(Rows.Count, 2).End(xlUp).Row
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you have no data in the column then this
Code:
Cells(Rows.Count, 2).End(xlUp)
will take you to the top row in the sheet, ie row 1
 
Upvote 0
That suggests that B1 contains 87800
 
Upvote 0
Assuming there are no formulas in Column B, this code snippet will report the number of cells in Column B that contain data (empty cells will not be counted)...
Code:
[table="width: 500"]
[tr]
	[td]  Dim FilledCellCount As Long
  On Error Resume Next
  FilledCellCount = Columns("B").SpecialCells(xlConstants).Count
  On Error GoTo 0
  MsgBox "Number of Rows with data in Column B is " & FilledCellCount & "."[/td]
[/tr]
[/table]
 
Upvote 0
Bill, what result does the below give?
Code:
Sub test()
MsgBox Cells(Rows.Count, 2).End(xlUp).Value
MsgBox Cells(Rows.Count, 2).End(xlUp).Value2
End Sub
 
Upvote 0
What does this return
Code:
Sub chk()
MsgBox "Number of Rows with data in Column b is" & " " & [counta(B:B)]
End Sub
Also what results do you get from Rick's & Mark's suggestions?
 
Upvote 0
Modified it a bit to exclude the first row or Header

FilledCellCount = Columns("B").SpecialCells(xlConstants).Count - 1
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,561
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top