Ok. You are getting mixed up between three different things. Arrays,
Variants containing arrays, and arrays of Variants.
An array is something defined like this
Dim x(10) as Long
A variant is defined like this
Dim x as Variant
I'll come to variants containing arrays in a moment
More inline...
Greg Maxey said:
Jonathan,
I solved my immediate problem with the below code. I was searching Google
and found the following tip that Jezebel had posted in response to a
similiar question:
"Put your arguments into an array, and pass that as a single argument. "
I knew I had the array and so I removed the "()" following vArray in the
Function you provided
What you were trying to pass was a Variant containing an array, whereas I
had defined an array of variants...
This only confirms my near complete lack of understanding of 1) Arrays, 2)
Passing arguments. I really don't understand the basic concept and
usually simply stumble on a workable solution. If you (or any other
benevolent soul) have time, I would certainly appreciate it if you could
provide a sample of how you intended your code to be employed with an
explanation of how it works. Of all the thing is VBA, it is those empty
parens "()" that confound me the most. I feel that I am close to an
epiphany but I could use divine intervention. Thanks for everything
Sub CallMacro()
Dim myArray
That defines a Variant. If you don't specify a datatype in a Dim statement,
your variable is created as a Variant. A variant is a kind of container
datatype which can be just about anything - an integer, a floating point
number, a string, or even an array of something.
Dim oMaxValue As Long
Dim oMinValue As Long
Dim i As Long
myArray = Array(3, 1, 13)
The Array function returns a Variant containing an array. What you have here
is almost identical to the code example in the VBA Help.
oMaxValue = MaxOfArray(myArray)
oMinValue = MinOfArray(myArray)
In both these lines, you are passing that Variant, which just so happens at
the moment to be containing an array.
MsgBox oMaxValue
MsgBox oMinValue
End Sub
Function MaxOfArray(vArray As Variant) As Long
Because you are passing a Variant to this function, your were getting a type
mismatch when you used my original code, because I has specificed the
paremater as follows
Function MaxOfArray(vArray() As Variant) As Variant
There are two key differences here
1. vArray vs vArray()
vArray means a variant is being passed. Remember that a variant can contain
anything including an array. vArray() means that an array of variants is
being passed. (and just to make live interesting, since each item in that
array is itself a Variant, it could be anything, including another array!
2. As Long vs As Variant
What comes after As determines the data type of the value returned by the
function. You have specified this as Long. This means you would have had
some strange results had you tried to call the function as follows. Try it
and see what happens!
myArray = Array(3.5, 1.9, 13.34)
oMaxValue = MaxOfArray(myArray)