Sub test()
Dim a, x, y, t, i As Long, flg As Boolean
Application.ScreenUpdating = False
With Sheets("bom").Cells(1).CurrentRegion
x = Filter(.Parent.[transpose(if((isnumber(search("SECTION",c3:c10000)))+(row(3:10000)=3)+(row(3:10000)=max(if(c3:c10000<>"",row(3:10000)))),row(3:10000)))], False, 0)
a = .Resize(x(UBound(x))).Value
For i = 0 To UBound(x) - 1
If UCase$(.Cells(x(i), 3)) Like "SECTION *" Then
.Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
.Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & x(i + 1) - 1 & "c)"
Else
If .Cells(x(i), 3) Like "OPTION *" Then
y = .Parent.Evaluate("min(if((isnumber(search(""SECTION"",c3:c10000)))*(row(3:10000)>" & x(i) & "),row(3:10000)))")
If y = 0 Then y = UBound(a, 1)
.Rows(x(i)).Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
.Rows(x(i)).Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i) + 1 & "c:r" & y - 1 & "c)"
End If
End If
Next
With CreateObject("Scripting.Dictionary")
For i = UBound(a, 1) To 3 Step -1
If a(i, 1) <> "" Then
If Not .exists(a(i, 1)) Then .Item(a(i, 1)) = Array(0, i, i)
If a(i, 3) Like "OPTION *" Then
.Item(a(i, 1)) = Array(.Item(a(i, 1))(0), i, .Item(a(i, 1))(2))
ElseIf a(i, 3) Like "SECTION *" Then
.Item(a(i, 1)) = Array(i, .Item(a(i, 1))(1), .Item(a(i, 1))(2))
End If
End If
Next
x = .items
End With
For i = 0 To UBound(x)
If i > 0 Then .Rows(x(i)(2) + 1).Resize(2).Insert: t = x(i)(2) + 1
If x(i)(1) <> x(i)(2) Then
.Rows(x(i)(1)).Resize(2).Insert: t = x(i)(1)
Else
t = x(i)(2) + 1
End If
With .Rows(t)
.Font.Bold = True: .Interior.Color = "&H8CFF"
.Range("d1") = "TOTAL": .Range("d1").HorizontalAlignment = xlRight
.Range("o1:p1").FormulaR1C1 = Array("=(rc[-1]-rc7)/rc[-1]", "=(rc[-2]-rc9)/rc[-2]")
.Range("g1,i1,k1,n1").FormulaR1C1 = "=subtotal(9,r" & x(i)(0) & "c:r[-1]c)"
End With
Next
End With
Application.ScreenUpdating = True
End Sub
Bookmarks