答案 0 :(得分:0)
一个简单的VBA sub do-until with will do,like this
index = 1 ' Initialize row index.
Do Until index > 7 ' Change 7 to .usedrange.Rows.Count / your number of rows.
If Range("A1").Offset(index - 1, 0).Value <> vbNullString Then
sum = Range("B1").Offset(index - 1, 0).Value
position = index + 1
' Do a running sum, until encounter the next non-blank
Do Until Range("A1").Offset(position - 1, 0) <> vbNullString
sum = sum + Range("B1").Offset(position - 1, 0).Value
position = position + 1
If position > 7 Then Exit Do ' Just in case.
Loop
Range("C1").Offset(index - 1, 0).Value = sum
' Reassign pointer
index = position
End If
Loop
答案 1 :(得分:0)
逻辑:检查Inv
,如果为空,请总结Qty
。如果没有,这意味着我们遇到了新的Inv
。因此,我们将总和存储到最后Total
并读取新Inv
的数据。
Private Sub Total()
With ActiveSheet
Dim lastRow As Long
lastRow = .Cells(Rows.Count, 2).End(xlUp).Row
Dim i As Long
Dim sum As Long
Dim firstInvCell As Range
On Error Resume Next
For i = 2 To lastRow
If .Cells(i, 1).Value = "" Then
'Inv is blank, sum up
sum = sum + .Cells(i, 2).Value
Else
'Inv is not blank, means next inv starts from here
firstInvCell.Value = sum
'store the first row of this inv
Set firstInvCell = .Cells(i, 3)
sum = .Cells(i, 2).Value
End If
Next i
firstInvCell.Value = sum
End With
End Sub