I have the following code which is meant to obtain the sheet names and put them into an array:
Function GetSheetNames(WBook) As String()
Dim TempArray() As String
NoSheets = WBook.Sheets.Count
ReDim TempArray(1 To NoSheets)
For i = 1 To NoSheets
TempArray(i) = WBook.Sheets(i).Name
Next i
GetSheetNames = TempArray
End Function
I tried to refactor the code to get rid of the TempArray variable:
Function GetSheetNames(WBook) As String()
NoSheets = WBook.Sheets.Count
ReDim GetSheetNames(1 To NoSheets)
For i = 1 To NoSheets
GetSheetNames(i) = WBook.Sheets(i).Name
Next i
End Function
However it returns an error. Is such thing even possible in VBA? Do I always need a temporary local variable inside a function which at the end gets assigned to the function name in order to return it?