Home |
Search |
Today's Posts |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Paul,
To explain whats happening: A variant containing an array (or a range) is not the same as an array of variants. If you want to access the parameter as a variant array just assign it to a variant. Public Function GoodBadFunction(avVariant As Variant) As Variant Dim vVar As Variant Debug.Print TypeName(avVariant) vVar = avVariant Debug.Print TypeName(vVar) GoodBadFunction = vVar Debug.Print TypeName(GoodBadFunction) End Function What happens is that when a variant parameter is passed a range, the variant is treated as containing a range, and when that variant containing a range is assigned to another variant, the range is converted to a variant containing an array of variants. (the exact treatment differs slightly by VBA version). However Excel will not let you pass a range to a variant array parameter (avVariant() as variant). This is also important for performance reasons: converting a range to a variant containing an array of variants with a single assignment statement is very efficient compared to reading the elements of the range one--by-one, but if you only want a few elements of the range (typically things like binary search or excel functions like MATCH and VLOOKUP) then it is much faster not to assign it to a variant but to handle it as a variant containing a range. I assume that using a paramarray involves each individual parameter being handled internally as a variant which is then assigned to a single element (as an array of variants) of the variant array that the function sees. so the paramarray winds up as an array of variants each element of which can contain an array. Charles ______________________ Decision Models FastExcel Version 2 now available. www.DecisionModels.com "Charles Williams" wrote in message ... It works OK if you use as variant rather than as variant(), and also handles non-range arguments. Public Function GoodBadFunction(avVariant as Variant) as String goodBadFunction= TypeName(avVariant) End Function regds Charles ______________________ Decision Models FastExcel Version 2 now available. www.DecisionModels.com "Paul S Bryant" wrote in message om... I agree that the elements in the Variant() array in the GoodFunction() are indeed of type Range. This wouldn't help me pass data to DotNet without the PIA, but it does confirm the thing I am immediately interested in. That is, the Excel function bar can't pass parameters to functions requiring variant arrays unless you use the ParamArray modifier. "Tom Ogilvy" wrote in message ... I believe your interpretation of GoodFunction is incorrect. If you test the typename of the first element of avVariant, it will be Range. No conversion has taken place. Replace the argument with a single number, and as written you will get the same result. =GoodFunction(21) Your interpretation of BadFunction is correct in that you have a mismatch with argument type and the function is never entered. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Opening XLS file directly and getting params | Excel Discussion (Misc queries) | |||
OWC load add-ins programmatically in dotnet? | Excel Discussion (Misc queries) | |||
Variant to String | Excel Programming | |||
Excel Workbook opened twice with dotnet interop | Excel Programming | |||
DLLs and VBA: Who free's a variant? | Excel Programming |