what is wrong with this single line of code (finding last row in a column)

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Code:
Dim cntRows As Long
cntRows = ActiveWorkbook.Worksheets("ANALYTICS").Cells("F1").Find("*", , , , xlByRows, xlPrevious, , , False).Row

Trying to find the last row in Column F that is not blank.

I get 'Invalid procedure call of argument' (run-time error '5': )
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try using range, instead of cells...
Code:
Dim cntRows As Long
cntRows = ActiveWorkbook.Worksheets("ANALYTICS").Range("F1").Find("*", , , , xlByRows, xlPrevious, , , False).Row

Incidentally, if it's just the last used row you need, then you can use:
Code:
Dim cntRows As Long
cntRows = ActiveWorkbook.Worksheets("ANALYTICS").Range("F65536").end(xlup).Row
 
Last edited:
Upvote 0
Try using range, instead of cells...
Code:
Dim cntRows As Long
cntRows = ActiveWorkbook.Worksheets("ANALYTICS").Range("F1").Find("*", , , , xlByRows, xlPrevious, , , False).Row

Incidentally, if it's just the last used row you need, then you can use:
Code:
Dim cntRows As Long
cntRows = ActiveWorkbook.Worksheets("ANALYTICS").Range("F65536").end(xlup).Row

Thank you, the second one worked for what I need.

Coincidentally, the first code returns a value of "1" (when I use MsgBox to show me the value of the variable) when there was actually 23 rows in column F.

The second code returns the value I was expecting (23)

Why would the first code only show "1"?

Thanks again, sykes.
 
Upvote 0
It Returns 1, because you are only searching one cell (namely F1)
 
Upvote 0
It Returns 1, because you are only searching one cell (namely F1)

Thank you, Fluff.

How would I go about searching starting from F1 and down to the last cell? (the 2nd code sykes provided works fine, but I would like to understand how to locate the last row going from the top and down instead of going the very last cell on the sheet and searching up...) Thank you
 
Upvote 0
Like
Code:
Range("F1").End(xlDown).Offset(1).Row
But if you have any blank cells in col F you might get the wrong result
 
Upvote 0
Like
Code:
Range("F1").End(xlDown).Offset(1).Row
But if you have any blank cells in col F you might get the wrong result

That's it. Thank you. & FWIW I dont have any blanks. Column E does have blanks in it, but the procedure right before this one, I had it copy just the cells with content from column E and paste that over to column F cell 1. thanks again for your help.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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