Tableaux et doublons

Bonjour

j'ai crée un bout de code permettant la suppression de doublons sur un tableau de type array : le voici

Dim t As Variant
t = Array("a", "b", "f", "k", "b", "c", "q", "f", "u")
For i = 0 To UBound(t)
 For j = i + 1 To UBound(t)
   If t(i) = t(j) Then
   t(j) = ""
   z = z & " " & t(i)
   End If
  Next
 Next
 'MsgBox z  'liste des doublons
 For i = 0 To UBound(t)
 y = y & " " & t(i)
 Next
 'MsgBox y 'liste des valeurs avec trous

 s = Split(y, " ")
  For k = 0 To UBound(s)
  If s(k) <> "" Then
  d = d & " " & s(k)
  End If
  Next
 MsgBox d 'liste des valeurs sans doublons

Il marche , mais Il y a t il moyen de faire plus court ? Merci pour vos réponses

bonjour

2 propositions

Sub aargh()
    Dim t As Variant
    t = Array("a", "b", "f", "k", "b", "c", "q", "f", "u")
    For i = 0 To UBound(t) - 1
        For j = i + 1 To UBound(t)
            If t(i) = t(j) Then t(j) = "": Z = Z & " " & t(i)
        Next
    Next
    MsgBox Z    'liste valeurs avec doublons
    For k = 0 To UBound(t)
        If t(k) <> "" Then d = d & " " & t(k)
    Next
    MsgBox d    'liste des valeurs sans doublons
End Sub

Sub aargh1()
    Dim t As Variant
    Set dico = CreateObject("scripting.dictionary")
    t = Array("a", "b", "f", "k", "b", "c", "q", "f", "u")
    For i = 0 To UBound(t) - 1
        If dico.exists(t(i)) Then Z = Z & " " & t(i) Else dico.Add t(i), 1
    Next
    MsgBox Z    'liste valeurs avec doublons
    t = dico.keys
    For k = 0 To UBound(t)
        If t(k) <> "" Then d = d & " " & t(k)
    Next
    MsgBox d    'liste des valeurs sans doublons
End Sub

Avec un peu de retard , un grand merci h2So4 je vais regarder la seconde methode qui a l'air pas mal

Rechercher des sujets similaires à "tableaux doublons"