Private Sub CommandButton2_Click()
On Error Resume Next
If Datetr = "" Then
Datetr.SetFocus
MsgBox "Vous devez donner une date."
Exit Sub
ElseIf Not IsDate(Datetr) Then
Datetr = ""
Datetr.SetFocus
MsgBox "Date incorrecte."
Exit Sub
End If
If OptionButton1 = False And OptionButton2 = False Then
MsgBox "Vous devez choisir une option : Entrees ou Sorties."
Exit Sub
End If
If ListBox1.ListCount = 0 Then
MsgBox "Le dossier d'échange est vide, il ne contient aucun élément ", , "alerte"
Exit Sub
End If
Dim dat, dat_bon As Date
Dim x As Integer
Dim v As Integer
Dim Uf As Integer
Dim i As Long
Dim J As Long
Set fa = Sheets("Stock")
With Feuil1
Uf = fa.Range("A" & Rows.Count).End(xlUp).Row
For i = 0 To ListBox1.ListCount - 1
For J = 2 To Uf
If .Cells(J, 1) = Val(ListBox1.List(i, 0)) And .Cells(J, 5) = ComboBox1 Then
If Me.OptionButton1 = True Or Me.OptionButton2 = True Then
dat_bon = .Cells(J, 9)
If dat_bon < dat Then
dat_bon = dat
End If
End If
End If
Next J
Next i
For i = 0 To ListBox1.ListCount - 1
For J = 2 To Uf
If .Cells(J, 1) = Val(ListBox1.List(i, 0)) And .Cells(J, 5) = ComboBox1 And .Cells(J, 9) = dat_bon Then
If Me.OptionButton1 = True Then
.Cells(J, 4) = .Cells(J, 4) + Val(ListBox1.List(i, 3))
.Cells(J, 6) = .Cells(J, 6) - Val(ListBox1.List(i, 3))
ElseIf Me.OptionButton2 = True Then
.Cells(J, 4) = .Cells(J, 4) - Val(ListBox1.List(i, 3))
.Cells(J, 7) = .Cells(J, 7) + Val(ListBox1.List(i, 3))
End If
End If
Next J
Next i
End With
'Call TO_Maghzin
If OptionButton1 = True Then
Set fe = Sheets("Entrees")
For v = 0 To ListBox1.ListCount - 1
Lige = fe.Range("A" & Rows.Count).End(xlUp)(2).Row
fe.Range("A" & Lige) = Datetr
fe.Range("B" & Lige) = ListBox1.List(v, 0)
fe.Range("C" & Lige) = ListBox1.List(v, 2)
fe.Range("E" & Lige) = ListBox1.List(v, 3)
fe.Range("G" & Lige) = ListBox1.List(v, 4)
If Me.OptionButton1 = True Then
fe.Range("D" & Lige) = "Entrée"
ElseIf Me.OptionButton2 = True Then
fe.Range("D" & Lige) = "Sortie"
End If
fe.Range("F" & Lige) = ComboBox1
Next v
ElseIf Me.OptionButton2 = True Then
Set fs = Sheets("Sorties")
For x = 0 To ListBox1.ListCount - 1
Ligs = fs.Range("A" & Rows.Count).End(xlUp)(2).Row
fs.Range("A" & Ligs) = Datetr
fs.Range("B" & Ligs) = ListBox1.List(x, 0)
fs.Range("C" & Ligs) = ListBox1.List(x, 2)
fs.Range("E" & Ligs) = ListBox1.List(x, 3)
fs.Range("G" & Ligs) = ListBox1.List(x, 4)
If Me.OptionButton1 = True Then
fs.Range("D" & Ligs) = "Entrée"
ElseIf Me.OptionButton2 = True Then
fs.Range("D" & Ligs) = "Sortie"
End If
fs.Range("F" & Ligs) = ComboBox1
Next x
End If
Me.Quantitetr = ""
Me.ComboBox1 = ""
MsgBox "Saisie enregistrée."
ListBox1.Clear
Unload Me
UserForm1.Show
End Sub