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, U
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 42)
Dim coll() As Collection
ReDim coll(0 To 41)
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
Set coll(7) = New Collection ' Colonne U
Set coll(8) = New Collection ' Colonne V
Set coll(9) = New Collection ' Colonne W
Set coll(10) = New Collection ' Colonne X
Set coll(11) = New Collection ' Colonne Y
Set coll(12) = New Collection ' Colonne Z
Set coll(13) = New Collection ' Colonne AA
Set coll(14) = New Collection ' Colonne AB
Set coll(15) = New Collection ' Colonne AC
Set coll(16) = New Collection ' Colonne AD
Set coll(17) = New Collection ' Colonne AE
Set coll(18) = New Collection ' Colonne AF
Set coll(19) = New Collection ' Colonne AG
Set coll(20) = New Collection ' Colonne AH
Set coll(21) = New Collection ' Colonne AI
Set coll(22) = New Collection ' Colonne AJ
Set coll(23) = New Collection ' Colonne AK
Set coll(24) = New Collection ' Colonne AL
Set coll(25) = New Collection ' Colonne AM
Set coll(26) = New Collection ' Colonne AN
Set coll(27) = New Collection ' Colonne AO
Set coll(28) = New Collection ' Colonne AP
Set coll(29) = New Collection ' Colonne AQ
Set coll(30) = New Collection ' Colonne AR
Set coll(31) = New Collection ' Colonne AS
Set coll(32) = New Collection ' Colonne AT
Set coll(33) = New Collection ' Colonne AU
Set coll(34) = New Collection ' Colonne AV
Set coll(35) = New Collection ' Colonne AW
Set coll(36) = New Collection ' Colonne AX
Set coll(37) = New Collection ' Colonne AY
Set coll(38) = New Collection ' Colonne AZ
Set coll(39) = New Collection ' Colonne BA
Set coll(40) = New Collection ' Colonne BB
Set coll(41) = New Collection ' ColonneBC
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
coll(7).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 21) ' U
coll(8).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 22) ' O
coll(9).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 23) ' P
coll(10).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 24) ' Q
coll(11).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 25) ' R
coll(12).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 26) ' S
coll(13).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 27) ' T
coll(14).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 28) ' U
coll(15).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 29) ' O
coll(16).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 30) ' P
coll(17).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 31) ' Q
coll(18).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 32) ' R
coll(19).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 33) ' S
coll(20).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 34) ' T
coll(21).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 35) ' U
coll(22).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 36) ' O
coll(23).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 37) ' P
coll(24).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 38) ' Q
coll(25).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 39) ' R
coll(26).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 40) ' S
coll(27).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 41) ' T
coll(28).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 42) ' U
coll(29).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 43) ' O
coll(30).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 44) ' P
coll(31).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 45) ' Q
coll(32).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 46) ' R
coll(33).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 47) ' S
coll(34).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 48) ' T
coll(35).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 49) ' U
coll(36).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 50) ' O
coll(37).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 51) ' P
coll(38).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 52) ' Q
coll(39).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 53) ' R
coll(40).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 54) ' S
coll(41).Add Item:=0, key:=Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 55) ' 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))
Tres(Val.Row - 1, 8) = coll(7).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 21))
Tres(Val.Row - 1, 9) = coll(8).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 22))
Tres(Val.Row - 1, 10) = coll(9).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 23))
Tres(Val.Row - 1, 11) = coll(10).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 24))
Tres(Val.Row - 1, 12) = coll(11).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 25))
Tres(Val.Row - 1, 13) = coll(12).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 26))
Tres(Val.Row - 1, 14) = coll(13).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 27))
Tres(Val.Row - 1, 15) = coll(14).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 28))
Tres(Val.Row - 1, 16) = coll(15).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 29))
Tres(Val.Row - 1, 17) = coll(16).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 30))
Tres(Val.Row - 1, 18) = coll(17).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 31))
Tres(Val.Row - 1, 19) = coll(18).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 32))
Tres(Val.Row - 1, 20) = coll(19).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 33))
Tres(Val.Row - 1, 21) = coll(20).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 34))
Tres(Val.Row - 1, 22) = coll(21).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 35))
Tres(Val.Row - 1, 23) = coll(22).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 36))
Tres(Val.Row - 1, 24) = coll(23).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 37))
Tres(Val.Row - 1, 25) = coll(24).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 38))
Tres(Val.Row - 1, 26) = coll(25).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 39))
Tres(Val.Row - 1, 27) = coll(26).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 40))
Tres(Val.Row - 1, 28) = coll(27).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 41))
Tres(Val.Row - 1, 29) = coll(28).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 42))
Tres(Val.Row - 1, 30) = coll(29).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 43))
Tres(Val.Row - 1, 31) = coll(30).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 44))
Tres(Val.Row - 1, 32) = coll(31).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 45))
Tres(Val.Row - 1, 33) = coll(32).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 46))
Tres(Val.Row - 1, 34) = coll(33).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 47))
Tres(Val.Row - 1, 35) = coll(34).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 48))
Tres(Val.Row - 1, 36) = coll(35).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 49))
Tres(Val.Row - 1, 37) = coll(36).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 50))
Tres(Val.Row - 1, 38) = coll(37).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 51))
Tres(Val.Row - 1, 39) = coll(38).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 52))
Tres(Val.Row - 1, 40) = coll(39).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 53))
Tres(Val.Row - 1, 41) = coll(40).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 54))
Tres(Val.Row - 1, 42) = coll(41).Item(Val.Text & "-" & Val.Offset(, 3).Text & "-" & Val.Offset(, 6).Text & "-" & FRes.Cells(1, 55))
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