Find Last Row

detriez

Board Regular
Joined
Sep 13, 2011
Messages
193
Office Version
  1. 365
Platform
  1. Windows
I have multiple Subs that require finding the last row of my sheet


I use this over and over

VBA Code:
Dim lr As Long
    lr = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
    Application.ScreenUpdating = False

I've added this module. How would I replace the above with this function?
VBA Code:
Private Sub sLastRow()

Dim Last_Row As Long
   
On Error Resume Next
Last_Row = Cells.Find(What:="*", _
                After:=Range("A1"), _
                LookIn:=xlFormulas, _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious).Row
On Error GoTo 0
   
Debug.Print Last_Row

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about
VBA Code:
Function sLastRow() As Long

Dim Last_Row As Long
   
On Error Resume Next
Last_Row = Cells.Find(What:="*", _
                After:=Range("A1"), _
                LookIn:=xlFormulas, _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious).Row
On Error GoTo 0
   
If Last_Row = 0 Then sLastRow = 1 Else sLastRow = Last_Row

End Function
Sub chk2()
   Dim Lr As Long
   Lr = sLastRow
   Debug.Print Lr
End Sub
 
Upvote 0
Solution
Try

VBA Code:
Function LastRow(Optional ByVal sh As Object) As Long
    If sh Is Nothing Then Set sh = ActiveSheet
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, _
                            SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    On Error GoTo 0
End Function


VBA Code:
  Dim lr As Long
    lr = LastRow(ActiveSheet) + 1
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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