Sub Totaux()
Dim plage As Range, ncol As Integer, i As Long
Set plage = Range("C3", Cells(Rows.Count, "C").End(xlUp))
ncol = Cells(2, Columns.Count).End(xlToLeft).Column - 4
Application.ScreenUpdating = False
'---suppression des lignes TOTAL et S/TOTAL---
plage.AutoFilter 1, "*TOTAL*"
plage.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
plage.AutoFilter
'---création de la ligne TOTAL---
Set plage = plage.Resize(plage.Rows.Count + 1) 'avec ligne vide
With plage(plage.Rows.Count)
.Offset(-1).EntireRow.Copy .EntireRow 'pour les formats
.EntireRow.ClearContents
.Value = "TOTAL"
.Offset(, 2).Resize(, ncol).FormulaR1C1 = _
"=SUM(R4C:R[-1]C)-SUMIF(R4C3:R[-1]C3,""S/*"",R4C:R[-1]C)"
End With
'---insertion des lignes S/TOTAL---
For i = plage.Rows.Count To 3 Step -1
If plage(i) <> plage(i - 1) Then
plage(i).EntireRow.Insert
plage(i) = "S/TOTAL " & plage(i - 1)
plage(i).Offset(, 2).Resize(, ncol).FormulaR1C1 = _
"=SUM(R4C:R[-1]C)-2*SUMIF(R4C3:R[-1]C3,""S/*"",R4C:R[-1]C)"
End If
Next
'--suppression des formules (facultatif)---
plage.Resize(, ncol + 2) = plage.Resize(, ncol + 2).Value
End Sub