Public Function EliminerDoublons(ByRef Tableau1() As String) As Long ' ' Prend un tableau de chaines et détruit les doublons ' ' Retourne le nombre de doublons éliminés ' Dim Cmpt1 As Long ' Boucle Dim Cmpt2 As Long ' Boucle 2 Dim strItem As String ' L'item EliminerDoublons = UBound(Tableau1) 'On additionne pour chaque item distinct et recopie dans le même tableau à l'indice Cmpt2 For Cmpt2 = 0 To (EliminerDoublons - 1) strItem = Tableau1(Cmpt2) If (strItem <> vbNullString) Then 'Si cet item n'as pas déjà été compilé... ' On compare l'item tableau1(Cmpt2) au reste de la liste. For Cmpt1 = (Cmpt2 + 1) To EliminerDoublons If (StrComp(Tableau1(Cmpt1), strItem, vbTextCompare) = 0) Then Tableau1(Cmpt1) = vbNullString 'Else End If Next Cmpt1 End If Next Cmpt2 'On enlève les éléments vides... Call CompresserListeStr(Tableau1) EliminerDoublons = EliminerDoublons - UBound(Tableau1) End Function
Public Sub CompresserListeStr(ByRef InListe() As String) ' ' InListe contient un tableau d'items dont certains ' peuvent être Null. Cette fonction recopie les items de ' façon à placer toutes les chaines vide à la fin et ' réduire la taille du tableau. ' Dim Nombre As Long Dim Cmpt As Long Dim Cmpt2 As Long Nombre = UBound(InListe) 'Compresser la liste en déplacant les items vers le haut... Do While (Cmpt < Nombre) If (InListe(Cmpt) <> vbNullString) Then Cmpt = Cmpt + 1 Else Cmpt2 = Cmpt Do While ((InListe(Cmpt) = vbNullString) And (Cmpt < Nombre)) Cmpt = Cmpt + 1 Loop InListe(Cmpt2) = InListe(Cmpt) InListe(Cmpt) = vbNullString If (Cmpt <> Nombre) Then Cmpt = Cmpt2 + 1 End If Loop 'Trouver la première ligne blanche et couper. Cmpt = -1 Cmpt2 = -1 Do While ((Cmpt < Nombre) And (Cmpt2 = -1)) Cmpt = Cmpt + 1 If InListe(Cmpt) vbNullString Then Cmpt2 Cmpt Loop If (Cmpt2 = -1) Then 'Cas "Aucun blanc" Cmpt2 = Nombre Else 'Cas au moins un blanc Cmpt2 = Cmpt2 - 1 'Mais si rien dans liste, en principe impossible, forcer 0 pour éviter de planter If (Cmpt2 < 0) Then Cmpt2 = 0 End If ReDim Preserve InListe(Cmpt2) End Sub
Call DoubleAuSimple(Noms, NomUnique) Public Sub DoubleAuSimple(ByRef InTbl() As String, ByRef OutTbl() as String) Dim iCmpt as Integer Dim iNbr as Integer iNbr = UBound(InTbl) Redim OutTbl(iNbr) For iCmpt = 0 to iNbr OutTbl(iCmpt) = InTbl(iCmpt) Next iCmpt Call EliminerDoublons(OutTbl) End Sub