Excel VBA – 将二维数组的值分配给单元格范围

我试图给一个新的工作表分配一个大小为183×6的二维数组,用数组的值填充A1:G182中的空白单元格。 作为参考,我的数组被称为“目录”,我要填充的空表被称为“列表”。

我已经尝试了两种不同的方法,一种是通过明确地将指定的范围分配给数组:

Worksheets("List").Range(Cells(1,1), Cells(UBound(Directory, 1) + 1, UBound(Directory, 2) + 1)) = Directory 

还试图遍历数组中的每个条目:

 For i = 0 To UBound(Directory, 1) For j = 0 To UBound(Directory, 2) Worksheets("List").Range(Cells(i + 1, j + 1), Cells(i + 1, j + 1)) = Directory(i,j) Next j Next i 

在这两种情况下,我得到的错误:

 Run-time error '1004': Application-defined or object defined error. 

任何想法可能发生什么? 我感谢您的帮助。

尝试:

 Worksheets("List").Range("A1").Resize(UBound(Directory, 1) + 1, UBound(Directory, 2) + 1).Value = Directory 

要么:

 For i = 0 To UBound(Directory, 1) For j = 0 To UBound(Directory, 2) Worksheets("List").Range(Worksheets("List").Cells(i + 1, j + 1), Worksheets("List").Cells(i + 1, j + 1)) = Directory(i,j) Next j Next i 

您不需要任何循环将数组移动到内存。 例如:

 Sub Array2Range() Dim Directory(1 To 182, 1 To 6) As Variant Dim rng As Range, i As Long, j As Long For i = 1 To 6 For j = 1 To 182 Directory(j, i) = i * j Next j Next i Set rng = Sheets("List").Range("A1:F182") rng = Directory End Sub 

会产生:

在这里输入图像说明