I'm trying to write a vba function in Excel that can pass both object and scalar types. I can get the function to work properly by using a Variant as the signature, however, I cannot get the returned value from where it is called due to objects needing "set" and scalars not needing it. I wrote the following test case below. Can someone help me get it working, please?
I'm working on my own version of a JSON class module. To "load" the external data I have a reader which uses a recursive function which needs to do this (in case you were wondering).
EDIT: I cannot call the function twice like how QHarr suggested. QHarr method does work with the code below, however, this is not the full code, just a sample. Two reasons, 1) the function reads external data and calling this twice loses the current line being read, and 2) the actual function is recursive so calling it like this would result in unpredictable ways.
Sub test_passing_scalars_and_objects()
Dim i As Long
Dim var As Variant
For i = 0 To 3
Set var = pass_it(i)
Debug.Print "cleared index:=" & i
Next i
End Sub
Function pass_it(ByVal val As Long) As Variant
Dim d As New Scripting.Dictionary
Dim c As New Collection
Dim arr() As Variant
Select Case val
Case Is = 0 ' dictionary
d.Add "101", 101
d.Add "202", 202
Set pass_it = d
Case Is = 1 ' collection
c.Add "101"
c.Add "202"
Set pass_it = c
Case Is = 2 ' array
ReDim arr(1)
arr(0) = "101"
arr(1) = "202"
pass_it = arr
Case Is = 3 ' scalar
pass_it = "101"
End Select
End Function