-
Optional argument in a function error
I'm trying to write a function to retrieve last used cell, but get "Object variable or with block not defined" error...
[VBA]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[/VBA]
This is what I use to test it:
[VBA]Sub test3()
MsgBox GetLastCell()
End Sub[/VBA]
It works well if I define the argument in the function, but screws me with default value, can anyone help?
-
My_Sheet can't be optional. You'll have to pass it to be able to use it in the function.
-
-
To test if an optional function argument has been passed, one should either use a Variant argument and use IsMissing or should test the argument against a null.
[VBA]Function GetLastCell(Optional my_sheet As Worksheet)
Dim Cell As Integer
If my_sheet Is Nothing Then
GetLastCell = ActiveSheet.UsedRange.Rows.Count
Else
GetLastCell = my_sheet.UsedRange.Rows.Count
End If
End Function[/VBA]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules