I’ve been trying out various ways of finding the last row in a range and returning this number to a subroutine, to no avail.
Here’s the code I have
Sub StartHere()
Dim oSheet As Worksheet
Set oSheet = WorkSheets(1)
ProcessData(oSheet)
End Sub
Sub ProcessData(ByVal wkst As Worksheet)
Dim rng As Range
Dim lastRow As Long
'set range
Set rng = wkst.Range("L:S") 'Range that i want to process data on
'get the last row (in Long datatype)
lastRow = CLng(getLastRowInRange(rng))
End Sub
Function getLastRowInRange(ByRef rng As Range)
getLastRowInRange = rng.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,_ SearchDirection:=xlPrevious).Row
End Function
I keep getting Type Mismatch on the
getLastRowInRange = rng.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,_ SearchDirection:=xlPrevious).Row
Any clues, guys?
Remove the parentheses.
Otherwise you are passing the value of the default property of the
oSheetobject, which it doesn’t have. You should be getting “Object doesn’t support this property or method” though.After doing that, change the function call:
Aftermust belong to the search range.A1doesn’t belong toL:S.Finally, make sure you handle the case when
FindreturnsNothing(which you don’t at the moment, happily calling.Rowon the returned object).