1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| Sub test()
Dim Tab_Already() As Integer, Tab_Resultats()
Dim Tab_ColonneBD
Dim TheCell As Range
Dim IRowP As Long, IRowS As Long, IRowM As Long 'P=Primaire, S=Secondaire, M=Maxi
Dim OneInt
Dim StrConcat As String
'On initialise les tableaux
ReDim Tab_Already(0)
ReDim Tab_Resultats(1, 0)
Tab_ColonneBD = Range("A2", Cells(Rows.Count, "B").End(xlUp)).Value
IRowM = Cells(Rows.Count, "A").End(xlUp).Row - 1
For IRowP = 1 To IRowM
'On verifie que le numero n'a pas deja ete traité
For Each OneInt In Tab_Already
If OneInt = CInt(Tab_ColonneBD(IRowP, 1)) Then GoTo suite
Next
'On rajoute le numero dans la liste des num deja traité (la colonne A ne doit pas contenir de 0
If Tab_Already(UBound(Tab_Already)) <> 0 Then ReDim Preserve Tab_Already(UBound(Tab_Already) + 1)
Tab_Already(UBound(Tab_Already)) = CInt(Tab_ColonneBD(IRowP, 1))
'On initialise le concatenation avec le 1er texte
StrConcat = CStr(Tab_ColonneBD(IRowP, 2))
'On recherche les autres cellules contenant cette valeur à partir de IRowP+1
For IRowS = IRowP + 1 To IRowM
If Tab_ColonneBD(IRowP, 1) = Tab_ColonneBD(IRowS, 1) Then
StrConcat = StrConcat + " "
StrConcat = StrConcat + CStr(Tab_ColonneBD(IRowS, 2))
End If
Next
'On ajoute ce resultat au tableau des resultats
If Tab_Resultats(1, UBound(Tab_Resultats, 2)) <> "" Then ReDim Preserve Tab_Resultats(1, UBound(Tab_Resultats, 2) + 1)
Tab_Resultats(0, UBound(Tab_Resultats, 2)) = CInt(Tab_ColonneBD(IRowP, 1))
Tab_Resultats(1, UBound(Tab_Resultats, 2)) = StrConcat
suite:
Next
'On affiche le resultat, par exemple sur une feuille 2
Feuil2.Range("A2").Resize(UBound(Tab_Resultats, 2), 2).Value = WorksheetFunction.Transpose(Tab_Resultats)
End Sub |
Partager