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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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,224,824
Messages
6,181,186
Members
453,020
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