This question has no satisfactory answer. I will be happy to vote for any new responses that contain workarounds.
How to pass array variable to Excel named range without prior dumping it to cells? I want such a dynamic named range to be used in data validation list. I do not want to set up validation list directly in VBA through the array variable:
Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(ReturnArr, ",")
because of limitation of the length of Formula1
parameter to 255 characters. So the above code works fine as long as the Len(Join(ReturnArr, ","))
does not exceed 255 characters.
I have tried this:
Option Explicit
Function ReturnArr() As Variant
Dim Arr(0 To 2)
Arr(0) = "Spinosaur"
Arr(1) = "T-Rex"
Arr(2) = "Triceratops"
ReturnArr = Arr
End Function
Then I add a named range pointing to the function returning array:
Everything seems to be ok so far. I set up data validation list.
I have also tried the following paths with no success:
- transposing the array:
ReturnArr = Application.Transpose(Arr)
INDEX(ReturnArr(),,1)
- named range with
=OFFSET(ReturnArr(),0,0,ROWS(ReturnArr()),1)
- I event descended to such low level as to take use of one cell and I put the formula
=OFFSET(ReturnArr(),0,0,ROWS(ReturnArr()),1)
intoA1
cell and referenced to it withINDIRECT("$A$1")
- I experimented with undocumented secret
EVALUATE
function which works with named ranges as in this case. I triedEVALUATE
with all combinations of other functions returning array.
Further references:
Fast way to output array into range for those who would like to contribute to dumping approch