填充多维数组 [英] Fill multidimensional array
问题描述
我想知道如何在Excel VBA中填充多维数组。可以填充一个1d数组,如下所示:
I was wondering how to fill a multi-dimensional array in Excel VBA. A 1d array can be filled as follows:
Dim myarray as variant
myarray = Array("sheep", "goat", "chicken")
维度数组?
推荐答案
您可以这样做:
Dim a
a = [{1,2;3,4;5,6}]
限制:
-
只适用于
类型的数组
,因为[x]
是Evaluate(x)
的缩写,这意味着x
通过Excel解释,Excel只返回变体。所以声明Dim a As Variant
或数组Dim a()As Variant
工作正常。但不包括任何其他类型的阵列Dim a()As String
failed。
This only works with arrays of type
Variant
, because[x]
is shorthand forEvaluate("x")
which means thatx
is interpreted via Excel, and Excel only returns Variants. So declaringDim a As Variant
or an arrayDim a() As Variant
works fine. But not any other type of array e.g.Dim a() As String
fails.
这仅适用于一种特定类型的多维数组,即两个维数组。不是三维,四维等。
This only works for one specific kind of multi-dimensional array, namely two-dimensional arrays. Not three-, four- etc. dimensional.
这篇关于填充多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!