LastRow VBA

animas

Active Member
Joined
Sep 28, 2009
Messages
396
I need to use a function like
=LastRow(B2:B100)
will return B25 (B26 is last non empty cell within range B2:B100)

=LastRow(B:B)
will return B207 (B207 is last non empty cell within range B:B)

I was trying below which is not working

Code:
Public Function LastRow(iRange As Range)
    LastRow = Range(iRange).Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Address
End Function

Any solution?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
No need for a function, I handle it in the procedure

dim lr as long
lr = ThisWorkbook.Sheets("Sheet1").Range("B1").End(xlDown).Row

That should do you, edit to suit your workbook.
 
Upvote 0
If you want to return the address of the last cell in a range try this:
Code:
Function lastCell(iRange As Range) As String
Dim LastRow As Long, LastCol As Long
' Error-handling is here in case there is not any
' data in the range.
On Error Resume Next
With iRange
' Find the last row
    LastRow = .Cells.Find(what:="*", _
      searchdirection:=xlPrevious, _
      searchorder:=xlByRows).Row
  ' Find the last column
    LastCol = .Cells.Find(what:="*", _
      searchdirection:=xlPrevious, _
      searchorder:=xlByColumns).Column
    If Err.Number <> 0 Then 'worksheet is empty
       MsgBox ("Worksheet " & ws.Name & " is empty (all cells are blank).")
        lastCell = .Cells(1, 1).Address
    Else
       lastCell = .Cells(LastRow, LastCol).Address
    End If
End With
End Function
 
Upvote 0
Something like this should work. Function returns an error value if there is more one column in the range. It also checks in case the range selected is in a block of data in which case the last row should be returned.

Code:
Function LastRow(myRange As Range) As Long
    
    With myRange
        If .Columns.Count > 1 Then
            'more than one column
            LastRow = 99999999
        ElseIf Cells(.Rows.Count + .Row - 1, .Column) <> "" Then
            'last row contains data
            LastRow = .Rows.Count + .Row - 1
        Else
            LastRow = Cells(.Rows.Count + .Row - 1, .Column).End(xlUp).Row
        End If
    End With

End Function
 
Upvote 0
I was looking for returning last cell address within a range. JoeMo code is giving wrong addresses.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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