Liliana said:
The #REF! error indicates a problem referring to the the address
returned by the function. . . .
Technically, your udf returns a worksheet name followed by an
exclamation point.
I am unable to reproduce the problem. . . .
....
Really?! The OP's problem is immediately reproducible. Did you try
using worksheet names like the OP indicated? That is, worksheet names
that look like dates in dd-mm-yy format.
Excel *REQUIRES* single quotes around worksheet names containing
hyphens, spaces, commas, slashes, etc. Safest *ALWAYS* to enclose
worksheet names in single quotes since Excel ignores them when they're
unnecessary but chokes when they're necessary but not provided.
Personally, I think something like
Function wso(r As Range, Optional ByVal n As Long = 0) As Variant
With r.Parent.Parent.Worksheets
n = n + r.Parent.Index
If n >= 1 And n <= .Count Then
Set wso = .Item(n).Range(r.Address)
Else
wso = CVErr(xlErrRef)
End If
End With
End Function
would be more useful, where the usage would be something like
=wso(A5,-1)
to return the value of the A5 cell in the preceding worksheet. This
also allows more flexibility since, e.g., the formula
=wso(SheetX!A5,-2)
would return the value of the A5 cell in the worksheet 2 before SheetX
which could be anywhere with respect the worksheet in which you're
entering this formula. This also makes it easier to use fully or
partially relative addresses, and it avoids the volatile INDIRECT
function.
This doesn't include wrapping. That's intentional on my part.