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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| Sub totaux()
' je suppose que ton tableaux commence par la 2ligne et 2 eme colonne
' sinon change le I et le Z
Dim i, j, z As Integer
Dim etat
i = 2
j = 1
etat = 0
While Range("B" & i) <> "" ' je test si les sous totaux sont affichés
If Range("B" & i) = "Sous Total" Then
etat = 1
End If
i = i + 1
Wend
If etat = 1 Then ' je supprime les sous totaux
i = 2
While Range("B" & i) <> ""
If Range("B" & i) = "Sous Total" Then
Rows(i).Select
Selection.Delete Shift:=xlUp
i = i - 1
End If
i = i + 1
Wend
Else
i = 2
While Range("B" & i) <> ""
If Range("B" & i) <> Range("B" & i + 1) Then
i = i + 1
Rows(i).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 'j'ajoute une ligne
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
For z = 3 To 8 'je suppose que tu as au maximum 6 colonnes si plus change le 8
Cells(i, 2) = "Sous Total"
Cells(i, z).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-" & j & "]C:R[-1]C)" 'j'affecte la somme
Next z
j = 1
Else
j = j + 1
End If
i = i + 1
Wend
End If
End Sub |
Partager