I am trying to figure out a way to check if a Worksheet variable has been inizialized/assigned.
I am returning an object Worksheet from a function, and I need to handle errors.
What happens is that if an error occurs, the worksheet is not assigned and the variable that is supposed to get the value when calling the function raises an error "Object variable or With block variable not set"
I am returning an object Worksheet from a function, and I need to handle errors.
What happens is that if an error occurs, the worksheet is not assigned and the variable that is supposed to get the value when calling the function raises an error "Object variable or With block variable not set"
VBA Code:
Dim readSheet As Worksheet
readSheet = getWorkSheet(fileName)
VBA Code:
Function getWorkSheet(path As String) As Worksheet
Dim wb As Workbook
On Error GoTo handleOpen
Set wb = Workbooks.Open(path, ReadOnly = False)
getWorkSheet = wb.Worksheets(1)
wb.Close savechanges = False
Exit Function
handleOpen:
wb.Close savechanges = False
'I need to handle the getWorksheet variable not inizialized here
End Function