Sub sum_groups()
Dim CurrentCell As Range, CurrentRange As Range, rg As Range
Dim LastRow As Long
Dim ColorIndex As Long
LastRow = ActiveSheet.UsedRange.Rows.Count
ColorIndex = 6
Set CurrentCell = ActiveSheet.Range("A2")
Set CurrentRange = CurrentCell
'Loop until end of list
Do Until CurrentCell.Row = LastRow
'Loop until 0 group is found
Do Until WorksheetFunction.sum(CurrentRange) = 0
'Break loop if last row is reached
If CurrentCell.Row + CurrentRange.Rows.Count > LastRow Then Exit Do
Set CurrentRange = CurrentRange.Resize(CurrentRange.Rows.Count + 1)
Loop
If WorksheetFunction.sum(CurrentRange) = 0 Then
'Alternate color
If ColorIndex = 6 Then
ColorIndex = 4
ElseIf ColorIndex = 4 Then
ColorIndex = 6
End If
'Enter 0 and change color
For Each rg In CurrentRange
rg.Interior.ColorIndex = ColorIndex
rg.Offset(0, 1) = 0
Next rg
'Move to next cell after current range
Set CurrentCell = CurrentCell.Offset(CurrentRange.Rows.Count)
Else
'Move to next cell after current tested cell
Set CurrentCell = CurrentCell.Offset(1)
End If
'Reset CurrentRange
Set CurrentRange = CurrentCell
Loop
End Sub
2条答案
按热度按时间ztyzrc3y1#
这将在两种颜色之间交替。数字列表应在A列中,0应在B列中。
dwbf0jvd2#
假设值的总和总是为零,你可以使用SCAN来获得一个累积和,MAP来获得
start
(零之后的第一个值,或者数组中的第一个值)和end
数组中的所有零:如果你想要一个计数器: