Option Explicit
Sub CorrigeErrRefCompactTableauMac()
Application.ScreenUpdating = False
Dim t As Single
t = Timer
' Feuil 1 Résultat : Colonne N, O, P, Q, R, S, T
Dim FRes As Worksheet
Set FRes = Worksheets("feuil1")
Dim ResPlage As Range
Set ResPlage = FRes.Range(FRes.Cells(2, 1), FRes.Cells(FRes.Cells(1048576, 1).End(xlUp).Row, FRes.Cells(1, 16384).End(xlToLeft).Column))
Dim Tres()
ReDim Tres(1 To ResPlage.Rows.Count, 1 To 7)
Dim coll() As Collection
ReDim coll(0 To 6)
Set coll(0) = New Collection ' Colonne N
Set coll(1) = New Collection ' Colonne 0
Set coll(2) = New Collection ' Colonne P
Set coll(3) = New Collection ' Colonne Q
Set coll(4) = New Collection ' Colonne R
Set coll(5) = New Collection ' Colonne S
Set coll(6) = New Collection ' Colonne T
Dim Val As Range
For Each Val In ResPlage.Resize(ResPlage.Rows.Count, 1)
coll(0).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text ' ......................... N
coll(1).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 15) ' O
coll(2).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 16) ' P
coll(3).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 17) ' Q
coll(4).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 18) ' R
coll(5).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 19) ' S
coll(6).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 20) ' T
Next
'
' -----------------------------------------------------------------------------------------------------------------------------------------------
' Feuil base : Sheet 1
Dim FBase As Worksheet
Set FBase = Worksheets("Sheet 1")
Dim Tbase() As Variant
Tbase = FBase.Range(FBase.Cells(2, 1), FBase.Cells(FBase.Cells(1048576, 1).End(xlUp).Row, FBase.Cells(1, 16384).End(xlToLeft).Column))
'
' -----------------------------------------------------------------------------------------------------------------------------------------------
' Resultat ' Colonne N
Dim Cle As Variant
Dim j As Byte
Dim i As Long
Dim key As String
Dim cpt As Long
For i = LBound(Tbase, 1) To UBound(Tbase, 1)
For j = LBound(coll) To UBound(coll)
' Test si la clé existe
key = Tbase(i, 2) & "-" & Tbase(i, 5) & "-" & Tbase(i, 8)
'Debug.Print Exists(coll, j, key)
'Debug.Print coll(j).Item(Tbase(i, 2) & "-" & Tbase(i, 5) & "-" & Tbase(i, 8))
If Exists(coll, j, key) = True Then
If Tbase(i, 2) & "-" & Tbase(i, 5) & "-" & Tbase(i, 8) = key Then
cpt = coll(j).Item(key) + 1
coll(j).Remove key
coll(j).Add Item:=cpt, key:=key
cpt = Empty: key = Empty
End If
End If
key = Tbase(i, 2) & "-" & Tbase(i, 5) & "-" & Tbase(i, 8) & "-" & Tbase(i, 11)
If Exists(coll, j, key) = True Then
If Tbase(i, 2) & "-" & Tbase(i, 5) & "-" & Tbase(i, 8) & "-" & Tbase(i, 11) = key Then ' Tbase(i, 11) = Colonne K:K
cpt = coll(j).Item(key) + 1
coll(j).Remove key
coll(j).Add Item:=cpt, key:=key
cpt = Empty: key = Empty
End If
End If
Next j
Next i
j = Empty: i = Empty
For Each Val In ResPlage.Resize(ResPlage.Rows.Count, 1)
Tres(Val.Row - 1, 1) = coll(0).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text)
Tres(Val.Row - 1, 2) = coll(1).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 15))
Tres(Val.Row - 1, 3) = coll(2).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 16))
Tres(Val.Row - 1, 4) = coll(3).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 17))
Tres(Val.Row - 1, 5) = coll(4).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 18))
Tres(Val.Row - 1, 6) = coll(5).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 19))
Tres(Val.Row - 1, 7) = coll(6).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 20))
Next
' resultat
FRes.Cells(2, 14).Resize(UBound(Tres, 1), UBound(Tres, 2)).ClearContents
FRes.Cells(2, 14).Resize(UBound(Tres, 1), UBound(Tres, 2)) = Tres
MsgBox Timer - t
Application.ScreenUpdating = True
End Sub
Function Exists(ByRef coll() As Collection, ByVal j As Byte, ByVal key As String) As Boolean
' Le code suivant vérifie si une clé existe
On Error GoTo EH
IsObject (coll(j).Item(key))
Exists = True
EH:
End Function