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
| Private Sub CommandButton1_Click()
Dim i As Integer, a As Integer, b As Integer
Dim SomA As Long, SomB As Long
Dim CB1 As String, CB2 As String, Code As String
CB1 = UserForm1.ComboBox2.Value
CB2 = UserForm1.ComboBox3.Value
For i = 2 To Sheets("total").Cells(Rows.Count, 1).End(xlUp).Row
Code = Sheets("total").Cells(i, 1).Value
SomA = 0
SomB = 0
With Sheets(CB1)
For a = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
If .Cells(a, 1) = Code Then
SomA = SomA + .Cells(a, 2).Value
End If
Next a
End With
With Sheets(CB2)
For b = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
If .Cells(b, 1) = Code Then
SomB = SomB + .Cells(b, 2).Value
End If
Next b
End With
With Sheets("total")
.Cells(i, 2) = SomA - SomB
End With
Next i
Unload Me
End Sub |
Partager