Row select not selecting all data

saracat2012

New Member
Joined
Jun 27, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I'm using a row counter to select all data in a column, but it's dropping a number of rows at the end. When I run the below macro, it returns the correct number:

Sub Count_Rows()

Range("b4").Select
lastrow = ActiveCell.CurrentRegion.Rows.Count
MsgBox (lastrow)

End Sub

However, when I run the next macro to select the cells in column B that have data, it's dropping several rows (3-5 that I've witnessed, depending on the spreadsheet). The next macro is:

Sub selectdata()

Range("b4").Select
lastrow = ActiveCell.CurrentRegion.Rows.Count
Range("b4:b" & lastrow).Select

End Sub

Do I have something incorrect?

TIA
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
That is because if you have blank rows, the count of rows will be less than the row number of your last populated cell in column B.
Try this instead:
VBA Code:
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Range("B4:B" & lastrow).Select
 
Upvote 0
Solution
That is because if you have blank rows, the count of rows will be less than the row number of your last populated cell in column B.
Try this instead:
VBA Code:
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Range("B4:B" & lastrow).Select
Much appreciated. This worked like a charm.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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