Sub Journal()
'se lance par Ctrl+A
Dim col%, F1 As Worksheet, F2 As Worksheet, nlig&, ncol%, t, t1(), t2()
Dim dneg As Object, dpos As Object, s$, i&, x$, dneg1 As Object, dpos1 As Object
Dim n&, flag As Boolean, n1&, j%, n2&
col = 4 'n° de colonne des montants, à adapter
Set F1 = Sheets("Journal") 'nom à adapter
Set F2 = Sheets("Supprimé") 'nom à adapter
With [A1].CurrentRegion
nlig = .Rows.Count
ncol = .Columns.Count
t = .Resize(nlig, ncol + 1) '1 colonne de plus
ReDim t1(1 To nlig, 1 To ncol)
ReDim t2(1 To nlig, 1 To ncol)
End With
'---comptage des négatifs et positifs---
Set dneg = CreateObject("Scripting.Dictionary")
Set dpos = CreateObject("Scripting.Dictionary")
s = Chr(1)
For i = 1 To nlig
x = t(i, 1) & s & Replace(t(i, col), "-", "")
t(i, ncol + 1) = x 'mémorisation
If t(i, col) < 0 Then dneg(x) = dneg(x) + 1 Else dpos(x) = dpos(x) + 1
Next
'---repérage des paires et remplissage des tableaux t1 et t2---
Set dneg1 = CreateObject("Scripting.Dictionary")
Set dpos1 = CreateObject("Scripting.Dictionary")
For i = 1 To nlig
x = t(i, ncol + 1)
n = IIf(dneg(x) < dpos(x), dneg(x), dpos(x))
flag = True
If t(i, col) < 0 Then
If dneg1(x) < n Then dneg1(x) = dneg1(x) + 1: flag = False 'repèrage
Else
If dpos1(x) < n Then dpos1(x) = dpos1(x) + 1: flag = False 'repèrage
End If
If flag Then
n1 = n1 + 1
For j = 1 To ncol: t1(n1, j) = t(i, j): Next
Else
n2 = n2 + 1
For j = 1 To ncol: t2(n2, j) = t(i, j): Next
End If
Next
'---restitution---
F1.[A1].Resize(n1, ncol) = t1
F1.Rows(n1 + 1 & ":" & F1.Rows.Count).Delete
If n2 Then F2.[A2].Resize(n2, ncol) = t2
F2.Rows(n2 + 2 & ":" & F2.Rows.Count).Delete
With F1.UsedRange: End With: With F2.UsedRange: End With
MsgBox "Les feuilles '" & F1.Name & "' et '" & F2.Name & "' ont été mises à jour"
End Sub