Help with Correct Call for Ron de Bruin's "RDB_Last" to Find Last Row in a Column

R

RocketDude

I want to use Ron de Bruin's RDB_Last function (see code below) to find the
last row with data in a column, but I'm not sure if my intended call will
work correctly, as my data starts in row B5.

Intended call:

RDB_Last(1, "B")

Or should I call if more like:

RDB_Last(1, "B5:B5000")

Thanks,

RocketDude
----------------------------------------------------------------------

Function RDB_Last(choice As Integer, rng As Range)
'Ron de Bruin, 5 May 2008
' 1 = last row
' 2 = last column
' 3 = last cell
Dim lrw As Long
Dim lcol As Integer

Select Case choice

Case 1:
On Error Resume Next
RDB_Last = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0

Case 2:
On Error Resume Next
RDB_Last = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0

Case 3:
On Error Resume Next
lrw = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0

On Error Resume Next
lcol = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0

On Error Resume Next
RDB_Last = rng.Parent.Cells(lrw, lcol).Address(False, False)
If Err.Number > 0 Then
RDB_Last = rng.Cells(1).Address(False, False)
Err.Clear
End If
On Error GoTo 0

End Select
End Function
 
R

Ron de Bruin

Hi RocketDude

Use this if your range not start in the first row

Sub LastRow_Example()
Dim LastRow As Long
Dim rng As Range

'Use a range on the sheet
Set rng = Sheets("Sheet1").Range("B5:B500")

' Find the last row
LastRow = Last(1, rng)

If LastRow = 0 Then
MsgBox "The range is empty"
Else
MsgBox LastRow
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top