1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| Sub FormulaSum()
Dim AllGpe, Grp, TA, DL&, DF_G As Boolean, VA, i&, ColGroupe As New Collection, Col_G, Vgpe, NomGpe$, Groupe$, Som$, SomTotal$
Grp = Array("GROUPE", "TOTAL GROUPE")
TA = Array("TA", "TOTAL TA")
AllGpe = Array(Grp, TA)
DL = Cells(Rows.Count, "B").End(xlUp).Row: DF_G = False
With ActiveSheet.Range(Cells(1), Cells(DL, "M"))
For Each AG In AllGpe
VA = .Columns.Item(2).Value
For i = 16 To UBound(VA)
If VA(i, 1) > "" Then
If VA(i, 1) Like AG(Abs(DF_G)) & "*" Then
If DF_G = False Then
NomGpe = VA(i, 1)
ColGroupe.Add VA(i, 1), NomGpe
DF_G = True
Else: DF_G = False
Groupe = ColGroupe(NomGpe) & " _ " & .Cells(i, 12).Address
ColGroupe.Remove NomGpe: ColGroupe.Add Groupe, NomGpe
End If
ElseIf DF_G = True Then
If Cells(i, 2).Interior.Color <> RGB(255, 255, 255) Then
Groupe = ColGroupe(NomGpe) & " _ " & .Cells(i, 12).Address
ColGroupe.Remove NomGpe: ColGroupe.Add Groupe, NomGpe
End If
End If
End If
Next
Application.ScreenUpdating = False
For Each Col_G In ColGroupe
Vgpe = Split(Col_G, " _ ")
For i = 1 To UBound(Vgpe) - 1
.Range(Vgpe(i)).FormulaLocal = "=Somme(" & .Range(Vgpe(i)).Offset(1).Address & ":" & .Range(Vgpe(i + 1)).Offset(-1).Address & ")"
Som = Som & "+" & Vgpe(i)
Next
.Range(Vgpe(UBound(Vgpe))).FormulaLocal = "=" & Mid(Som, 2)
SomTotal = SomTotal & "+" & Vgpe(UBound(Vgpe))
Som = ""
Next
.Cells(DL, 12).FormulaLocal = "=" & Mid(SomTotal, 2)
Application.ScreenUpdating = True
Set ColGroupe = Nothing
Next
End With
End Sub |
Partager