I am trying to use a function that will allow me to import a range from a previous worksheet. I am not sure that my current macro allows this. I can call the previous worksheet manually, but not with the function that I have.
Here is the function that works fine when I manually input the worksheet name:
=IFERROR(VLOOKUP(D3,Aug9Daily!$D$3:$F$50,3, FALSE),"")
Here is the function that I am attempting to use:
=IFERROR(VLOOKUP(D3,NextSheetName()$D$3:$F$50,3, FALSE),"")
This is the VBA that I am using for the NextSheetName macro:
Function NextSheetName(Optional WS As Worksheet = Nothing) As String
Application.Volatile True
Dim S As String
Dim Q As String
If IsObject(Application.Caller) = True Then
Set WS = Application.Caller.Worksheet
If WS.Index = WS.Parent.Sheets.Count Then
With Application.Caller.Worksheet.Parent.Worksheets
Set WS = .Item(1)
End With
Else
Set WS = WS.Next
End If
If InStr(1, WS.Name, " ", vbBinaryCompare) > 0 Then
Q = "'"
Else
Q = vbNullString
End If
Else
If WS Is Nothing Then
Set WS = ActiveSheet
End If
If WS.Index = WS.Parent.Worksheets.Count Then
With WS.Parent.Worksheets
Set WS = .Item(1)
End With
Else
Set WS = WS.Next
End If
Q = vbNullString
End If
NextSheetName = Q & WS.Name & Q
End Function
What am I doing wrong? Is there a better way to select a range from another worksheet dynamically?
Try this to see if it works for you to fix your function’s output:
And then you will need to concatinate the output inside of an indirect function like this: