It's possible to assign "whole" arrays "directly" to an Variant variable in VBA (usually used for reading/writing whole ranges e.g. varRange = Range("A1:A3").Value
):
Dim varVariable As Variant
varVariant = Array("a", "b", "c")
Dim arrVariant() As Variant
arrVariantVarSize = Array("d", "e", "f")
Is it possible to do that with an array consisting of a regular data type (not necessarily just string or integer)? Similar to this (which does not work since array() returns a variant array that can't be assigned to a string or integer array):
Dim arrString(2) As String
arrString = Array("a", "b", "c") '-> throws an exception
Dim arrInteger (2) As Integer
arrInteger = Array(1, 2, 3) '-> throws an exception
Instead of this:
Dim arrString(2) As String
arrString(0) = Array("a")
arrString(1) = Array("b")
arrString(2) = Array("c")
Dim arrInteger(2) As String
arrInteger(0) = Array(1)
arrInteger(1) = Array(2)
arrInteger(2) = Array(3)
Dim arrString As Variant
works - TheArray
function returns a variant. Is there a reason why this does not work for you?Array
- What's the problem with it being a variant?arrString = Split(Join(Array("a", "b", "c"),","),",")
but that's just ugly.Array
function always returns an array ofVariant
regardless if you pass arguments of typeString
or other types. For exampleArray("a", "b", "c")
andArray(1, 2, 3)
both return an array ofVariant
type. So, you cannot just assign it to an array ofString
type. All you can do is to assign to aVariant
or an array ofVariant
which is exactly why your first 2 examples work.