next
01-05-2011, 08:07 AM
I'm trying to write a function to retrieve last used cell, but get "Object variable or with block not defined" error...
Function GetLastCell(Optional my_sheet As Worksheet)
Dim Cell As Integer
If IsEmpty(my_sheet) Then
GetLastCell = ActiveSheet.UsedRange.Rows.Count
Else
GetLastCell = my_sheet.UsedRange.Rows.Count
End If
End Function
This is what I use to test it:
Sub test3()
MsgBox GetLastCell()
End Sub
It works well if I define the argument in the function, but screws me with default value, can anyone help?
Function GetLastCell(Optional my_sheet As Worksheet)
Dim Cell As Integer
If IsEmpty(my_sheet) Then
GetLastCell = ActiveSheet.UsedRange.Rows.Count
Else
GetLastCell = my_sheet.UsedRange.Rows.Count
End If
End Function
This is what I use to test it:
Sub test3()
MsgBox GetLastCell()
End Sub
It works well if I define the argument in the function, but screws me with default value, can anyone help?