Private Sub Worksheet_Activate()
Dim ncol%, d1 As Object, d2 As Object, resu(), tablo, i&, n&, x$, j%, y$, lig&, k&, c As Range
ncol = 1 'nombre de colonnes à comparer, à adapter
Set d1 = CreateObject("Scripting.Dictionary")
d1.CompareMode = vbTextCompare 'la casse est ignorée
Set d2 = CreateObject("Scripting.Dictionary")
d2.CompareMode = vbTextCompare 'la casse est ignorée
ReDim resu(1 To Rows.Count, 1 To 2 * ncol + 1)
tablo = Feuil1.[B2].CurrentRegion.Resize(, ncol + 1) 'matrice, plus rapide
For i = 2 To UBound(tablo)
n = n + 1
x = Trim(tablo(i, 1))
resu(n, 1) = x
For j = 2 To ncol + 1: resu(n, 2 * j - 2) = tablo(i, j): Next
d1(x) = d1(x) + 1 'compte
d2(x & Chr(1) & d1(x)) = n 'repère la ligne
Next i
tablo = Feuil2.[B2].CurrentRegion.Resize(, ncol + 2) 'matrice, plus rapide, 1 colonne de plus pour le repérage
d1.RemoveAll 'RAZ
For i = 2 To UBound(tablo)
x = Trim(tablo(i, 1))
d1(x) = d1(x) + 1 'compte
y = x & Chr(1) & d1(x)
If d2.exists(y) Then
lig = d2(y)
For j = 2 To ncol + 1: resu(lig, 2 * j - 1) = tablo(i, j): Next j
tablo(i, ncol + 2) = lig 'repère le numéro de ligne
Else
tablo(i, ncol + 2) = ""
End If
Next i
'---restitution---
Application.ScreenUpdating = False
If FilterMode Then ShowAllData 'si la feuille est filtrée
With [B5] '1ère cellule de restitution, à adapter
If n Then .Resize(n, 2 * ncol + 1) = resu
.Resize(Rows.Count - .Row + 1, 2 * ncol + 1).Font.Bold = False 'non gras
.Offset(n).Resize(Rows.Count - n - .Row + 1, 2 * ncol + 1).ClearContents 'RAZ en dessous
'---insertion des lignes du 2ème bilan non traitées---
For i = 2 To UBound(tablo)
If tablo(i, ncol + 2) = "" Then
lig = Val(tablo(i - 1, ncol + 2)) + 1
If lig > 1 Then .Cells(lig, 1).Resize(, 2 * ncol + 1).Insert xlDown
For j = 1 To ncol + 1: .Cells(lig, 2 * j - 1) = tablo(i, j): Next j
For k = i + 1 To UBound(tablo)
If tablo(k, ncol + 2) <> "" Then tablo(k, ncol + 2) = tablo(k, ncol + 2) + 1 'incrémente les repères en dessous
Next k
tablo(i, ncol + 2) = lig 'repère le numéro de ligne
n = n + 1
End If
Next i
'---traitement de la ligne Bilan---
If n Then Set c = .Resize(n).Find("Bilan", , xlValues, xlWhole)
If Not c Is Nothing Then
c.Resize(, 5).Cut
.Offset(n).Insert
.Offset(n - 1).Font.Bold = True 'gras
End If
End With
With UsedRange: End With 'actualise les barres de défilement
End Sub