PDA

View Full Version : Solved: Array Function?



MWE
06-08-2005, 05:26 PM
Can a function be defined as an array and take on multiple values?

For example,


Function A(arg1, arg2, arg2) As Integer()
Dim AA(3) As Integer
AA(1) = arg1 + arg2
AA(2) = arg1 * arg2
AA(3) = arg1 / arg3
A = AA
End Function

and, if so, how is the function referenced by the calling procedure?

xCav8r
06-08-2005, 06:21 PM
Function A(arg1, arg2, arg3) As Variant
Dim AA(3) As Integer
AA(1) = arg1 + arg2
AA(2) = arg1 * arg2
AA(3) = arg1 / arg3
A = AA
End Function

Sub TestMyFunction()
Dim varaMyArray As Variant
varaMyArray = A(1, 2, 3)
Debug.Print varaMyArray(1)
Debug.Print varaMyArray(2)
Debug.Print varaMyArray(3)
End Sub

MWE
06-08-2005, 07:25 PM
This was too easy. I had tried essentially the same code as in your TestMyFunction but had dimensioned varaMyArray.

Thanks

BlueCactus
06-08-2005, 07:29 PM
xCav8r has the answer for you. It's basically the only way of doing it (setting the function as a variant). You can't Dim or Redim the function with dimensions, but you can set the function to another array (as xCav8r shows, A= AA), use A = Array(x,y,z), or A = ActiveSheet.Cells(1,1).Resize(4,5) or whatever methods you know of for creating an array in a variant without explicitly dim'ing it as an array.