Supprimer doublon controle listbox

Résolu
steve077 Messages postés 6 Date d'inscription vendredi 29 décembre 2006 Statut Membre Dernière intervention 29 juin 2008 - 23 juin 2008 à 17:40
steve077 Messages postés 6 Date d'inscription vendredi 29 décembre 2006 Statut Membre Dernière intervention 29 juin 2008 - 23 juin 2008 à 18:44
Bonjour

Je voudrais supprimer les doublons dans un contrôle de 6 listbox nommées List1(0) ,List1(1) ... ,List1(5)
Le problème c'est que ma fonction tourne en boucle.
Je me suis inspiré de ce code : http://www.vbfrance.com/code.aspx?ID=7647

Je vous laisse mon code ci-dessous :

Function VerifDoublon()

Dim iPos As Integer
iPos = 0

 If Form2.List1(5).ListCount < 1 Then Exit Function    'Si la listbox est vide il quitte la fonction

    Do While iPos < Form2.List1(5).ListCount
        Form2.List1(5).Text = Form2.List1(5).List(iPos)

    'Verifie si le text existe deja
    If Form2.List1(5).ListIndex <> iPos Then
       If Form2.List1(4).ListIndex <> iPos Then
          If Form2.List1(3).ListIndex <> iPos Then
            If Form2.List1(2).ListIndex <> iPos Then
                If Form2.List1(1).ListIndex <> iPos Then
                   If Form2.List1(0).ListIndex <> iPos Then
                         'Si c 'est le cas il supprime et garde la position iPos...
                         Form2.List1(5).RemoveItem iPos
                         Form2.List1(4).RemoveItem iPos
                         Form2.List1(3).RemoveItem iPos
                         Form2.List1(2).RemoveItem iPos
                         Form2.List1(1).RemoveItem iPos
                         Form2.List1(0).RemoveItem iPos
                        Else
                         'Si ce n'est pas le cas il change la position iPos...
                         iPos = iPos + 1

              End If
          End If
       End If
    End If
  End If
End If
 Loop
 
 Form2.List1(5).Text = "-"   'Utilisé pour désélectionner la dernière ligne

End Function

Merci d'avance a ceux qui pourront m'aider

6 réponses

mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
23 juin 2008 à 17:46
salut,

utilise mon snippet :
Private Sub 
NoDupesInCombo(ByRef txt As ListBox)
    Dim i As Long, j As Long
   
    For i = 0 To txt.ListCount - 1
        For j = txt.ListCount - 1 To i + 1 Step -1
            If txt.List(i) = txt.List(j) Then txt.RemoveItem (j)
        Next j
    Next i
End Sub

t'as plus qu'à faire un
NoDupesInCombo Text1

@++

le mystérieux chevalier,"Provençal, le gaulois"
3
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
23 juin 2008 à 17:47
ouias enfin

NoDupesInCombo LIST1

(à l'origine, c'était pour un Combo, mais bon, j'ai mal corrigé)

@++

le mystérieux chevalier,"Provençal, le gaulois"
0
steve077 Messages postés 6 Date d'inscription vendredi 29 décembre 2006 Statut Membre Dernière intervention 29 juin 2008
23 juin 2008 à 18:00
Salut

J'ai réussi en modifiant ton code , merci Mortalino :)

Je met le code modifié si jamais quelqu'un à le même problème un jour :

 Sub VerifDoublon()
    Dim i As Long, j As Long
   
    For i = 0 To Form2.List1(5).ListCount - 1
        For j = Form2.List1(5).ListCount - 1 To i + 1 Step -1
            If Form2.List1(5).List(i) = Form2.List1(5).List(j) Then
                If Form2.List1(4).List(i) = Form2.List1(4).List(j) Then
                    If Form2.List1(3).List(i) = Form2.List1(3).List(j) Then
                        If Form2.List1(2).List(i) = Form2.List1(2).List(j) Then
                            If Form2.List1(1).List(i) = Form2.List1(1).List(j) Then
                                If Form2.List1(0).List(i) = Form2.List1(0).List(j) Then
                               
                                    Form2.List1(5).RemoveItem (j)
                                    Form2.List1(4).RemoveItem (j)
                                    Form2.List1(3).RemoveItem (j)
                                    Form2.List1(2).RemoveItem (j)
                                    Form2.List1(1).RemoveItem (j)
                                    Form2.List1(0).RemoveItem (j)
       
       
                                End If
                            End If
                        End If
                    End If
                End If
            End If
       
       
        Next j
    Next i
End Sub
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
23 juin 2008 à 18:03
Humm jute ça, ça devrait suffir :

Private Sub 
NoDupesInCombo(ByRef txt As ListBox)
    Dim i As Long, j As Long
   
    For i = 0 To txt.ListCount - 1
        For j = txt.ListCount - 1 To i + 1 Step -1
            If txt.List(i) = txt.List(j) Then txt.RemoveItem (j)
        Next j
    Next i
End Sub



 Sub VerifDoublon()








NoDupesInCombo






Form2.List1




End Sub


@++

le mystérieux chevalier,"Provençal, le gaulois"
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
23 juin 2008 à 18:04
put*** de mise en page

Private Sub NoDupesInCombo(ByRef txt As ListBox)
    Dim i As Long, j As Long
    
    For i = 0 To txt.ListCount - 1
        For j = txt.ListCount - 1 To i + 1
Step -1
            If txt.List(i) = txt.List(j) Then
txt.RemoveItem (j)
        Next j
    Next i
End Sub

Sub VerifDoublon()
        NoDupesInCombo Form2.List1
End Sub
~
<small>Mortalino ~ [code.aspx?ID=39466 Colorisation
automatique]</small>

là c'est mieux

@++

le mystérieux chevalier,"Provençal, le gaulois"
0
steve077 Messages postés 6 Date d'inscription vendredi 29 décembre 2006 Statut Membre Dernière intervention 29 juin 2008
23 juin 2008 à 18:44
Oui sa marche aussi merci a toi :)
0
Rejoignez-nous