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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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