Bonjour à tous
Moi je n’aurais fait qu’une seule boucle
exercice programmation (1).xlsm (28,2 Ko)
Option Explicit
Sub Boucle()
'********************************
'* DECLARATIONS DES VARIABLES *
'********************************
Dim Ws As Worksheet
Dim Wd As Worksheet
Dim Dl As Integer
Dim i As Integer
'********************************
'* INITIALISATION DES VARIABLES *
'********************************
Set Ws = Sheets("Feuil1")
Set Wd = Sheets("Feuil2")
Dl = Ws.Range("A" & Rows.Count).End(xlUp).Row
'********************************
'* MACRO *
'********************************
For i = 6 To 8
Wd.Range("E" & i) = Application.WorksheetFunction.SumIf(Ws.Range("A2:A" & Dl), Wd.Cells(i, 4), Ws.Range("B2:B" & Dl))
Next i
'********************************
'* VIDE LA MEMOIRE DES OBJETS *
'********************************
Set Ws = Nothing
Set Wd = Nothing
End Sub
Ou avec trois IF … THEN
Sub Boucle2()
'********************************
'* DECLARATIONS DES VARIABLES *
'********************************
Dim Ws As Worksheet
Dim Wd As Worksheet
Dim Dl As Integer
Dim i As Integer
'********************************
'* INITIALISATION DES VARIABLES *
'********************************
Set Ws = Sheets("Feuil1")
Set Wd = Sheets("Feuil2")
Dl = Ws.Range("A" & Rows.Count).End(xlUp).Row
'********************************
'* MACRO *
'********************************
For i = 2 To Dl
If Ws.Cells(i, 1) = Wd.Range("D6") Then Wd.Range("E6") = Wd.Range("E6") + Ws.Cells(i, 2)
If Ws.Cells(i, 1) = Wd.Range("D7") Then Wd.Range("E7") = Wd.Range("E7") + Ws.Cells(i, 2)
If Ws.Cells(i, 1) = Wd.Range("D8") Then Wd.Range("E8") = Wd.Range("E8") + Ws.Cells(i, 2)
Next i
'********************************
'* VIDE LA MEMOIRE DES OBJETS *
'********************************
Set Ws = Nothing
Set Wd = Nothing
End Sub