-1

sorry I haven’t found the way to solve this issue. When trying to define an array in this way

Dim Arr As Variant: Arr = [{"Value1", "Value2"}]

works perfectly, but when trying to do the same using variables (i.e.)

Dim Arr as variant
v1 = "Value1"
v2 = "Value2"
Arr = [{v1, v2}]

will throw a syntax error. Please is there some way to solve it? I do need to use variables to populate the array. Thanks in advance

JRP
  • 1
  • 2
    Have you looked up how to initialize an array in VBA? – fbueckert Apr 25 '19 at 16:17
  • 1
    square brackets in VBA is shorthand for `Application.Evaluate()` which means anything in the square brackets will be treated as **literal** text and evaluated as if it was entered into the formula bar. That's why you cannot use variables - initialise it as a proper `Array` object instead. – SierraOscar Apr 25 '19 at 16:29

1 Answers1

3
Arr = [{"Value1", "Value2"}]

is

Arr = Application.Evaluate("{""Value1"", ""Value2""}")

You are making the Excel formula engine to parse the expression, and it parses it as an array constant.
Obviously the formula engine knows nothing about VBA variables.

You should simply use

Arr = Array(v1, v2)
GSerg
  • 76,472
  • 17
  • 159
  • 346