Option Explicit
Dim Tbl, Stk%, TQE%, TQS%, n2&, k1&, k2&
Private Sub BlocES()
Cells(k1, 3) = "Entrée": Cells(k2, 3) = "Sortie": Cells(k1, 4).Resize(2) = 0: n2 = n2 + 3
End Sub
Private Sub MvtX(lg&)
Dim Mvt$, Qté%, b As Byte: Mvt = Tbl(lg, 3): Qté = Tbl(lg, 4)
If Mvt = "Entrée" Then
TQE = TQE + Qté: Stk = Stk + Qté: Cells(k1, 4) = TQE: b = 1
End If
If Mvt = "Sortie" Then
TQS = TQS + Qté: Stk = Stk - Qté: Cells(k2, 4) = TQS: b = 2
End If
If b > 0 Then Cells(k2, 6) = Stk: Tbl(lg, 5) = 1
End Sub
Sub RécapTyp()
If ActiveSheet.Name <> "Mvts" Then Exit Sub
Dim n1&: n1 = Cells(Rows.Count, 1).End(3).Row: If n1 = 1 Then Exit Sub
Dim sh As Worksheet, cel As Range, ref$, d1 As Date, d2 As Date, i&, j&, k&
Set sh = ActiveSheet: n1 = n1 - 1: Tbl = [A2].Resize(n1, 5): Application.ScreenUpdating = 0
ActiveCell.Select: Worksheets("TRT").Select: n2 = Cells(Rows.Count, 3).End(3).Row
If n2 > 1 Then [A2].Resize(n2 - 1, 7).ClearContents 'effacement des anciens résultats
n2 = 3 'les nouveaux résultats seront écrits à partir de la ligne n° 3
For i = 1 To n1
If Tbl(i, 5) = 0 Then
ref = Tbl(i, 1): j = sh.Columns(1).Find(ref, , -4163, 1, 1).Row - 1
If i = j Then
Cells(n2, 1) = ref: d1 = sh.Cells(i + 1, 2): Cells(n2, 2) = d1
Set cel = Worksheets("Stock").Columns(1).Find(ref, , -4163, 1, 1)
If Not cel Is Nothing Then
Stk = cel.Offset(, 1): Cells(n2, 5) = Stk: TQE = 0: TQS = 0
k1 = n2 + 1: k2 = n2 + 2: BlocES: MvtX i
For k = i + 1 To n1
If Tbl(k, 5) = 0 Then
If Tbl(k, 1) = ref Then
d2 = sh.Cells(k + 1, 2)
If d2 <> d1 Then k1 = k1 + 3: k2 = k2 + 3: Cells(k1, 2) = d2: d1 = d2: BlocES
MvtX k
End If
End If
Next k
cel.Offset(, 1) = Stk: n2 = n2 + 1
End If
End If
End If
Next i
End Sub