Option Explicit
Sub test()
Dim a, w, i As Long, ii As Byte
Dim txt As String
Dim dico1 As Object
Dim dico2 As Object
Dim arr As Variant
Dim asupprimer As Variant
Dim pos As Byte
Dim result As Variant
Dim item As Variant
Set dico1 = CreateObject("Scripting.Dictionary")
Set dico2 = CreateObject("Scripting.Dictionary")
' tableau de 120 lignes en Feuille 1
'120 = factorielle de 5
a = Sheets("Feuil1").[a1].CurrentRegion.Resize(, 5).Value
arr = Array(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
For i = 1 To UBound(a, 1)
txt = a(i, 1) ' clé des 2 dictionnaires
'txt = Join$(Array(a(i, 1), a(i, 2)), "")
If Not dico1.exists(txt) Then
' etape 1 et 2
asupprimer = Array(a(i, 1))
'asupprimer = Array(a(i, 1), a(i, 2))
pos = Application.Match(a(i, 1), arr, 0)
'pos = Application.Match(a(i, 2), arr, 0)
ReDim w(0 To UBound(arr) - pos + 1)
For ii = pos To UBound(arr) + 1
w(ii - pos) = arr(ii - 1)
Next
' etape 3
ReDim result(0 To UBound(w))
Dim index As Byte
index = 0
For Each item In w
If Not IsInArray(item, asupprimer) Then
result(index) = item
index = index + 1
End If
Next item
ReDim Preserve result(0 To index - 1)
' etape 4
' les 4 elements a repartir sur la 2eme colonne
ReDim Preserve result(0 To 3)
'Debug.Print Join$(Array(a(i, 1))); ""
'Debug.Print Join$(Array(a(i, 1), a(i, 2))); ""
'Debug.Print Join(result, ", "); ""
'on associe la variable tableau de 4 elements à la clé désignée
dico1(txt) = result
dico2(txt) = 0
End If
Next
' on remplit la colonne B
For i = 1 To UBound(a, 1)
txt = a(i, 1) ' la clé
If dico1.exists(txt) Then
'on récupère l'index de l'element à répartir
index = dico2(txt)
'on ecrit dans la cellule l'element désigné par l'index
Sheets("Feuil1").Cells(i, 2) = dico1(txt)(index)
index = index + 1
If index = 4 Then index = 0
'on associe le nouvel index à la clé pour répartir l'element suivant
dico2(txt) = index
End If
Next
Set dico1 = Nothing
Set dico2 = Nothing
End Sub