Find Last Used Row on Worksheet

EssoExplJoe

New Member
Joined
Nov 11, 2016
Messages
28
I am using the below code to find the last row that contains data on a worksheet. I have 50 rows that contain data on worksheet (ws) and when I run my macro using the below function to find the last used row, it returns 50 as it should. However, if I manually delete the data from the last 25 rows on the worksheet, the code still returns 50. How I get it to reset and return 25.. Note that there is a range variable set in the macro to include the number of used rows which is supposed to be reset each time the macro is run to the number of current used rows.


VBA Code:
Function FindLastRow(ws as worksheet) as long
FindLastRow= ws.UsedRange.Rows(sht.UsedRange.Rows.Count).Row
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
If column "A" is always <> "" for a non blank row:
FindLastRow= ws..Cells(ws.Rows.Count, 1).End(xlUp).Row
 
Upvote 0
How about
VBA Code:
Sub MM1()
Dim lr as long
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
End Sub
 
Upvote 0
How about
VBA Code:
Sub MM1()
Dim lr as long
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
End Sub
Works great for rows. I also tried the below code to find the last used column but it doesn't seem to work very well.

VBA Code:
Sub MM1()
Dim lc as long
lc = Cells.Find("*", , xlValues, , xlcolumns, xlPrevious).Column
End Sub
 
Upvote 0
Try using
VBA Code:
Sub MM1()
dim Lc as integer
Lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,376
Messages
6,119,179
Members
448,871
Latest member
hengshankouniuniu

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