Option Explicit
Sub example_2()
Dim i As Long, intRow As Long, r As Long, c As Long, r1 As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = Worksheets("AfterForTesting")
' Pass #1 - copy over and add Group totals and formating
With Worksheets("Before")
intRow = .Cells(.Rows.Count, 6).End(xlUp).Row
For r = 1 To intRow
ws.Cells(r, 6).Value = .Cells(r, 6).Value
ws.Cells(r, 8).Value = .Cells(r, 8).Value
ws.Cells(r, 9).Value = .Cells(r, 9).Value
If .Cells(r, 6).Value = "xxx" Then
r1 = r
ws.Cells(r, 8).Resize(1, 7).Interior.ColorIndex = 15
ws.Cells(r, 11).Resize(1, 4).Font.Bold = True
ws.Cells(r, 11).Resize(1, 4).Font.Italic = True
ElseIf .Cells(r, 6).Value = "yyy" Then
For c = 11 To 14
If .Cells(r, c).Value > 0 Then
ws.Cells(r1, c).Value = ws.Cells(r1, c).Value + .Cells(r, c).Value
ws.Cells(r, c).Value = .Cells(r, c).Value
End If
Next c
End If
Next r
End With
' Pass #2 - Insert Group-City lines and format
With ws
intRow = .Cells(.Rows.Count, 8).End(xlUp).Row
For r = intRow To 1 Step -1
If Len(.Cells(r, 9).Value) = 0 Then
.Cells(r + 1, 6).Resize(1, 9).Insert Shift:=xlDown
.Cells(r + 1, 6).Resize(1, 9).ClearFormats
.Cells(r + 1, 6).Resize(1, 9).Interior.ColorIndex = 6
.Cells(r + 1, 8).Value = .Cells(r, 8).Value & "City#1"
End If
Next r
End With
' Pass #3 - add Group-City totals and form
With ws
r1 = 0
intRow = .Cells(.Rows.Count, 6).End(xlUp).Row
For r = 1 To intRow
If Len(.Cells(r, 6).Value) = 0 Then
r1 = r
ElseIf r1 <> 0 Then
If .Cells(r, 9).Value = "City#1" Then
For c = 11 To 14
If .Cells(r, c).Value > 0 Then
.Cells(r1, c).Value = .Cells(r1, c).Value + .Cells(r, c).Value
End If
Next c
End If
End If
Next r
End With
Application.ScreenUpdating = True
End Sub