Finding the last used cell not giving the desired result

jhonty4

Board Regular
Joined
May 16, 2016
Messages
85
Hi
I have an autogenerated worksheet in sheet1 I am trying to create a macro for which I want to find the last used cell in column "D". The data in column D start from row 6 and when i am using the row.end method to find the last used row i am getting the value as 1 instead of 2843. The code I am using is:
Dim lnglastrow As Long
lnglastrow = Sheet1.Cells(Rows.Count, "D").End(xlUp).Rows
MsgBox lnglastrow

I have tried replacing "D" with "D6" but that gives error and also note that that this code is placed in PERSONAL XLSB folder.
TIA
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
If it is in the personal workbook then try

Code:
Dim lnglastrow As Long
 lnglastrow = ActiveWorkbook.Sheets("[COLOR="#FF0000"]YourSheetname[/COLOR]").Cells(Rows.Count, "D").End(xlUp).Row
 MsgBox lnglastrow

Change YourSheetname to suit.
 
Last edited:
Upvote 0
You're welcome, just remember that the sheets codename only applies to the same workbook that the code resides in.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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