[VBA] Example 2
Private gMaxRow As Integer
Private gRow As Integer
Public Function ClearLineSum(baseCol)
For row = 2 To gMaxRow
Cells(row, baseCol).Select
CellValue = Selection.Value
If IsEmpty(Cells(row, baseCol).Value) Then
Else
Cells(row, baseCol).Value = ""
Cells(row, baseCol + 1).Value = ""
End If
Next
End Function
Public Function CalcLineSum()
Dim ColIdx
Dim RowIdx
Dim PreIdx
Dim NowDir
Dim MaxCol
gMaxRow = ActiveSheet.UsedRange.Rows.Count
'gMaxRow = 100
gRow = 2
ColIdx = 1
ClearLineSum (ColIdx + 4)
PreValue = ""
Sum = 0
For row = 2 To gMaxRow
Cells(row, ColIdx).Select
CellValue = Selection.Value
If IsEmpty(CellValue) Then
Else
If PreValue <> CellValue Then
If PreValue <> "" Then
Cells(gRow, ColIdx + 4).Value = PreValue
Cells(gRow, ColIdx + 5).Value = Sum
Cells(gRow, ColIdx + 4).Interior.Color = RGB(0, 100 * ((gRow Mod 2) + 1), 0)
Cells(gRow, ColIdx + 5).Interior.Color = RGB(0, 100 * ((gRow Mod 2) + 1), 0)
gRow = gRow + 1
Sum = 0
End If
End If
Sum = Sum + Cells(row, ColIdx + 2).Value
End If
PreValue = CellValue
Next
' Last Sum
If Sum <> 0 Then
Cells(gRow, ColIdx + 4).Value = PreValue
Cells(gRow, ColIdx + 5).Value = Sum
Cells(gRow, ColIdx + 4).Interior.Color = RGB(0, 100 * ((gRow Mod 2) + 1), 0)
Cells(gRow, ColIdx + 5).Interior.Color = RGB(0, 100 * ((gRow Mod 2) + 1), 0)
End If
CalcSum = "Done"
gRow = 1
Cells(1, 1).Select
End Function