Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Excel VBA:如何将嵌套数组写入范围_Arrays_Excel_Vba_Multidimensional Array - Fatal编程技术网

Arrays Excel VBA:如何将嵌套数组写入范围

Arrays Excel VBA:如何将嵌套数组写入范围,arrays,excel,vba,multidimensional-array,Arrays,Excel,Vba,Multidimensional Array,我最近开始在Excel中使用嵌套数组。我在使用它们时遇到了一些问题 我通常用于快速将数组写入某个范围的方法不再有效。失败的原因似乎是因为我试图找到嵌套数组的维度的方式(请参见下面的代码)。我知道我可以使用嵌套for循环写出数组,但是有更好/更快的方法吗 Sub print_arr() Dim test_arr(1 to 10) as variant for i = 1 to 10 test_arr(i) = Array(1,2,3,4) next I 'sample Locals

我最近开始在Excel中使用嵌套数组。我在使用它们时遇到了一些问题

我通常用于快速将数组写入某个范围的方法不再有效。失败的原因似乎是因为我试图找到嵌套数组的维度的方式(请参见下面的代码)。我知道我可以使用嵌套for循环写出数组,但是有更好/更快的方法吗

Sub print_arr()

Dim test_arr(1 to 10) as variant

for i = 1 to 10
    test_arr(i) = Array(1,2,3,4)
next I

'sample Locals readout: test_arr(1)(0) = 1, test_arr(1)(1) = 2, test_arr(1)(2) = 3, test_arr(1)(3) = 4,

Dim Desination as Range
Set Destination = Range("A1")
'normally I would do something like
Destination.Resize(UBound(test_arr, 1), UBound(test_arr, 2)).Value = test_arr
'but this doesn't work for a nested array
'there is an issue with the ubound(test_arr,2) 
'so what's the best way to write out nested array to Destination?

End Sub
运行上述代码后,我希望看到的结果如下所示:

       A | B | C | D |
    ------------------
    1: 1 | 2 | 3 | 4 |
    2: 1 | 2 | 3 | 4 |
    3: 1 | 2 | 3 | 4 |
    4: 1 | 2 | 3 | 4 |
    5: 1 | 2 | 3 | 4 |
    6: 1 | 2 | 3 | 4 |
    7: 1 | 2 | 3 | 4 |
    8: 1 | 2 | 3 | 4 |
    9: 1 | 2 | 3 | 4 |
   10: 1 | 2 | 3 | 4 |

另一个相关的附带问题:检查test_arr(i)中的值是否为嵌套数组的最佳方法是什么?如果存在嵌套数组,则Ubound(test_arr(i))起作用,并给出预期值3。如果test_arr(i)不是嵌套数组或为空,则ubound(test_arr(i))会产生一个“类型不匹配”错误。

您是否真的不知道某个东西是二维数组还是数组?具有确定数组的维数的函数
Debug.Print TypeName(test_arr(1))类似“*()”
将告诉您数组插槽是否包含嵌套数组。我认为如果我想使用嵌套数组通过索引匹配来填充表,这可能是一种情况。由于索引匹配失败,该数组的某些部分可能为空。