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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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