How to get VBA Subroutine to call a function that passes an array to another function within the subroutine

user1902208 picture user1902208 · Dec 13, 2012 · Viewed 8.8k times · Source

I am trying to get my VBA subroutine to run a function that creates an array and then passes that array to another function which further processes those array items. I have made an example that is extremely simple and functions exactly like my actual code. it is as follows:

Sub SubRoutine()
ProcessArray CreateArray
End Sub
Function ProcessArray(Arr() As Variant) As Variant
End Function
Function CreateArray() As Variant
Dim Array1(1 To 4) As Variant
CreateArray = Array1
End Function

It's just the two functions and the subroutine which calls those two functions. The compiler refuses to compile my code and explains to me that:

Compile error:

Type mismatch: array or user-defined type expected

To which I can only say that everything is the same data type and the argument passed is indeed an array. Just in case you were wondering, yes I have tried with an array that has had data allocated to it.

I know it's something subtle I am missing in VBA syntax but I have not seen an example like this. Any insight would be greatly appreciated.

Answer

Daniel picture Daniel · Dec 13, 2012

Change this line:
Function ProcessArray(Arr() As Variant) As Variant
to this:
Function ProcessArray(Arr As Variant) As Variant

This way your function will now accept a Variant that contains an array, instead of looking for an array of Variants. As you said, a subtle but significant difference.