2017-11-25 58 views
0

Visual Representation of the arrays在陣列

嗨Excel VBA中的累計總和,

我剛開始用Excel VBA和我正在使用數組一個小項目。 Array1中的數據是靜態的。我正在尋找一種使用VBA填充Array2的方法。

對於在ARRAY2每個單元我想做到以下幾點:

數組2(ROW1)=數組1(ROW1)

...。

ARRAY2(行3)=數組1(ROW1 +行2 + ROW3)

...。

數組2(ROW5)=數組1(ROW1 +行2 + ... + ROW5)

如何翻譯這個操作VBA?

回答

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 
+1

Thx我從這段代碼開始,它工作的很棒! – JHS