Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 使用数组填充二维范围时输出为空_Arrays_Excel_Vba_Range - Fatal编程技术网

Arrays 使用数组填充二维范围时输出为空

Arrays 使用数组填充二维范围时输出为空,arrays,excel,vba,range,Arrays,Excel,Vba,Range,我试图用二维数组填充二维范围 目前,我有以下几点: Sub populate() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") Dim Rows(2) As Variant Rows(0) = [{1, 2, 3}] Rows(1) = [{4, 5, 6}] Rows(2) = [{7, 8, 9}] With ws .Range(.Cells(1, 1), .Cells(3, 3)).Value

我试图用二维数组填充二维范围

目前,我有以下几点:

Sub populate()

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Sheet1")

Dim Rows(2) As Variant

Rows(0) = [{1, 2, 3}]
Rows(1) = [{4, 5, 6}]
Rows(2) = [{7, 8, 9}]

With ws
    .Range(.Cells(1, 1), .Cells(3, 3)).Value = Rows
End With


End Sub
我没有得到任何错误等,但在运行宏后,范围仍然为空

最奇怪的是,当我这样做时,我得到了正确的输出:

With ws
    .Range(.Cells(1, 1), .Cells(3, 3)).Value = Application.Transpose(Application.transpose(Rows))
End With 

我阅读了许多文章和Microsoft文档,但没有找到任何方法来解决我的问题。

我不明白您为什么对2D数组中的2D数组感兴趣,但根据@Rory的评论,您可以尝试以下方法:

Sub populate()

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Blad1")

Dim Rows(2, 0) As Variant

Rows(0, 0) = [{1, 2, 3; 4, 5, 6; 7, 8, 9}]
Rows(1, 0) = [{1, 2, 3; 4, 5, 6; 7, 8, 9}]
Rows(2, 0) = [{1, 2, 3; 4, 5, 6; 7, 8, 9}]

'Rows(0,0)(1,1) = "value" 'If you want to change. This example will change the first element of the 2D-array within the first element of your first 2D-array.

With ws
    .Range(.Cells(1, 1), .Cells(3, 3)).Value = Application.Transpose(Application.Transpose(Rows(0, 0))) 'Will transpose your first element in your initial 2D-array (not sure if this is put correctly in English)
End With

End Sub
reasonArr(1, 0) = [{0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0}]
如果要对二维数组中的每个元素执行上述操作,则可以将For x=Lboundrows实现为Uboundrows

在聊天室中,OP显然还希望实现以下功能:

Sub populate()

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Blad1")

Dim Rows(2, 0) As Variant

Rows(0, 0) = [{1, 2, 3; 4, 5, 6; 7, 8, 9}]
Rows(1, 0) = [{1, 2, 3; 4, 5, 6; 7, 8, 9}]
Rows(2, 0) = [{1, 2, 3; 4, 5, 6; 7, 8, 9}]

'Rows(0,0)(1,1) = "value" 'If you want to change. This example will change the first element of the 2D-array within the first element of your first 2D-array.

With ws
    .Range(.Cells(1, 1), .Cells(3, 3)).Value = Application.Transpose(Application.Transpose(Rows(0, 0))) 'Will transpose your first element in your initial 2D-array (not sure if this is put correctly in English)
End With

End Sub
reasonArr(1, 0) = [{0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0}]

它抛出了一个错误。这是因为提供的求值行超过255个字符。

行是一维数组,其元素是一维数组。你需要一个真正的二维阵列Clare Dim Rows2,0作为变量你已经有了一个工作选项,所以我不太清楚你在寻找什么?您可以简单地创建一个二维数组,以:Dim Rows作为变量:Rows=[{1,2,3;4,5,6;7,8,9}]开始,如果您想使用Evaluate,那么可以尝试:Rows0,0=[Row1:3],Rows1,0=[Row4:6]和Rows2,0=[Row7:9]。这就是你想做的吗?检查locale variables(区域设置变量)窗口,查看所有不同的选项对数组的作用。根据我的最后一条评论,您可以通过Rows0,01,1=您的值添加一个值,这将更改2D数组第一个元素中第一个2D数组的第一个元素的值=…。嗯,是的,谢谢,但我已经得到了这个值。让我们继续聊天。