I need help with finding the last row in a worksheet range.
For example:
Worksheet name is MyWorksheet
Range is A1:A200
What im trying to find out is the row number of the last cell that has data in the MyWorksheet range A1:A200.
I need help with finding the last row in a worksheet range.
For example:
Worksheet name is MyWorksheet
Range is A1:A200
What im trying to find out is the row number of the last cell that has data in the MyWorksheet range A1:A200.
Define your Range, Set it and use .Find
Here is a good example of what you are trying to accomplish.
Excel VBA Find row number of matching value
Hope this helps.
Taking into account all possible data layouts, inside and outside the range of interest requires consideration of a number of factors
Function RangeLastRow(r As Range, _
Optional Col = 1, _
Optional RelativeToSheet As Boolean = False) As Long
Dim rw As Long
With r.Cells(r.Rows.Count, Col)
If IsEmpty(.Value2) Then
rw = .End(xlUp).Row
If rw < r.Row Then
rw = r.Row
End If
Else
rw = r.Row + r.Rows.Count - 1
End If
End With
If IsEmpty(r.Cells(rw - r.Row + 1, Col)) Then
' range is empty
rw = 0
Else
If Not RelativeToSheet Then
rw = rw - r.Row + 1
End If
End If
RangeLastRow = rw
End Function
Use it as a UDF on a sheet, or in VBA like this
Sub Demo()
Using default Column 1 and Relative to Sheet
MsgBox RangeLastRow(Worksheets("MyWorksheet").Range("A1:A200"))
End Sub