您好,
我刚开始使用Excel VBA,我正在开发一个带数组的小项目。 Array1中的数据是静态的。我正在寻找一种使用VBA填充Array2的方法。
对于Array2中的每个单元格,我想执行以下操作:
Array2(Row1)= Array1(Row1)
...
Array2(第3行)= Array1(Row1 + Row2 + Row3)
...
Array2(Row5)= Array1(Row1 + Row2 + ... + Row5)
如何将此操作转换为VBA?
答案 0 :(得分:1)
你有没有尝试过什么?这是一种方法。
Sub x()
Dim array1, array2() As Long, i As Long, j As Long, k As Long
array1 = Range("A1").CurrentRegion.Value 'array1 populated from sheet, but could be via code
ReDim array2(1 To UBound(array1, 1), 1 To UBound(array1, 2))
For i = LBound(array1, 1) To UBound(array1, 1)
k = k + 1
For j = LBound(array1, 2) To UBound(array1, 2)
array2(i, j) = Application.Sum(Application.Index(array1, Evaluate("ROW(1:" & k & ")"), j))
Next j
Next i
Range("I1").Resize(UBound(array2, 1), UBound(array2, 2)).Value = array2
End Sub