假设表1从A2到A102有101行数据
Sub mypro()
Dim arrA(100) As Single rem 定义数组arrA 来储存A列的数据
Dim arrB(100) As Single rem 定义数组arrB 来储存将要返回到B列的数据
Dim i As Integer
For i = 0 To 100
arrA(i) = Sheet1.Cells(i + 2, 1).Value rem 数组arrA赋值
Next
arrB(0) = arrA(0) rem B2的值等于初始值A2的值
For i = 1 To 100
If arrA(i) = arrA(i - 1) Then
arrB(i) = arrA(i) + arrB(i - 1) rem A3的值等于A2,那么B3的值就等于A3+B2
Else
arrB(i) = arrA(i) rem A3的值不等于A2,那么B3的值就等于A3
End If
Next
For i = 0 To 100
Sheet1.Cells(i + 2, 2).Value = arrB(i) rem 循环结束后一次性将结果返回到表里
End Sub