edit: solution - see original question below
In passing arrays like {1,2,3} to a UDF I found two things to be considered:
the locale - see answer 1. The list separator on a german system (usually) is ";" therefore I need to use {1 ; 2 ; 3}.
the passed array appears as a 2-dimensional array within the function. Therefore it's n-th element must be targeted as myArray(n, 1). Disregarding this gave me the #VALUE! error.
Thus, a short "select case"-UDF may look like this:
Function SelCase(a1, a2, a3)
For i = 1 To UBound(a2)
If a2(i, 1) = a1 Then SelCase = a3(i, 1)
Next
End Function
called like (german locale!):
=SelCase(A1;{1;2;3};{"a";"b";"c"})
giving "a", "b", or "c" in result depending on A1 holds 1, 2 or 3.
A more elaborated "select case"-UDF is found here.
original question:
I'd like to pass an array like {1,2,3,4} to a user defined function in Excel 2002 - and don't manage to find the way to do this.
I'm using a german version, so "," is my decimal separator and also separates values in an horizontal (1D-)array - edit: this is wrong - , wheras ";" separates arguments in functions called in formulas from sheet and also seprates values in vertical (1D-)arrays - as far as I know.
I tried something like
Function test(myArray)
Function test(myArray())
with something like
=test({1,2,3,4})
{=test({1,2,3,4})} (with ctrl+shift+enter)
in my sheet, but Excel alway asks me to correct my formula like "=test({1,234})" which is not what I want.
If I try something like
=test({1;2;3;4})
=test(1,2,3,4)
=test(1;2;3;4) <- ok this would be for paramArray
as formula in the sheet I get an #VALUE! error.
I can't use paramArray 'cause in the end I'll have to pass two arrays (with variable size) and one single value as 3 arguments. What syntax do I need in the sheet and in VBA to pass an array (which is not defined as a range)?
Thank you in advance! Martin Lindenlauf
edit:
What I'm trying to build is a shorthand UDF for "Select Case", like:
Function SelCase() As Variant
a1 = "b"
a2 = Array("a","b","c")
a3 = Array("e1","e2","e3")
For i = 0 To UBound(a2)
If a2(i) = a1 Then SelCase = a3(i)
Next
End Function
with a1, a2, a3 not defined within the function but passed by function call like
=SelCase(A1;{"a","b","c"};{"e1","e2","e3"})
giving "e1"..."e3" depending on A1 = "a", "b" or "c".
I could realize this with CHOOSE() and MATCH() but I neet it very often -> like to have a nice "short version", and btw. I'd like to understand what I'm doing wrong with arrays and UDFs... Thanks.
edit 2:
I found a working approach for "select case UDF" here. The general question remains: how to pass an array in a kind of {1,2,3}-notation to an UDF (if possible).