Re : Somme d'un champs dans une macro
Sayé j'ai trouvé en fouillant un peu partout et grace à vous internautes; voici le résultat de mon problème:
Dim derlign%, Lg%
Dim trouve As Range
derlign = Range("B" & Rows.Count).End(xlUp).Row 'trouve la dernière ligne de la colonne B
Set trouve = Range("B2:B" & derlign).Find("TR", lookAt:=xlWhole) 'on utilise la méthode Find pour chercher "TR" dans la colonne B
If Not trouve Is Nothing Then Rows(trouve.Row).Resize(2).Insert Shift:=xlDown
'--- 1er sous-total ---
Lg = trouve.Row
Cells(Lg - 2, "g") = "=SUM(g3:g" & Lg - 3 & ")"
Cells(Lg - 2, "h") = "=SUM(h3:h" & Lg - 3 & ")"
With Range("a" & Lg - 2 & ":h" & Lg - 1)
.BorderAround Weight:=xlMedium
.Interior.ColorIndex = 45
End With
With Cells(Lg - 2, "g")
.BorderAround Weight:=xlMedium
.Interior.ColorIndex = 43
End With
With Cells(Lg - 2, "h")
.BorderAround Weight:=xlMedium
.Interior.ColorIndex = 43
End With
'--- 2ème sous-total (à la fin) ---
derlign = Range("B" & Rows.Count).End(xlUp).Row + 1
Range("a" & Lg - 2 & ":h" & Lg - 1).Copy Destination:=Range("a" & derlign)
Cells(derlign, "g") = "=SUM(g" & Lg & ":g" & derlign - 1 & ")"
Cells(derlign, "h") = "=SUM(h" & Lg & ":h" & derlign - 1 & ")"
End Sub
Merci pour toutes tes remarques Dranreb, elles m'ont été très utiles