IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Tri dans listbox


Sujet :

Macros et VBA Excel

  1. #1
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 056
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 056
    Points : 174
    Points
    174
    Par défaut Tri dans listbox
    Bonjour, je cherche à trier les lignes d'une listbox.

    La listbox liste des valeurs dans les cellules d'une feuille de calculs que je ne souhaite pas modifier.

    Le tri consiste à trier ces valeurs en fonction de leurs occurrences dans la colonne concernée.
    Je place devant la valeur de la cellule récupérée (un texte) la valeur d'
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    application.worksheetfunction.countif
    puis un espace.

    Il y a plusieurs fois 1;13;2;34 ..... (valeurs prises au hasard pour illustrer).
    Le code que j'utilise classe effectivement mes valeurs mais selon un ordre 9;9;7;5;5;42;4;357;3...... et je voudrais savoir comment obtenir: 357;42;9;9;7;5;5.4;3........

    Le code utilisé:


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    Sub TriFichRec()
     
            Dim w As Integer, z As Integer, Temp As String
     
    For w = 0 To UserForm11.ListBox1.ListCount - 1
        For z = w + 1 To UserForm11.ListBox1.ListCount - 1
     
                Debug.Print Mid(UserForm11.ListBox1.List(w), 1, InStr(1, UserForm11.ListBox1.List(w), " ") - 1)
                Debug.Print UserForm11.ListBox1.List(w)
                Debug.Print Mid(UserForm11.ListBox1.List(z), 1, InStr(1, UserForm11.ListBox1.List(z), " ") - 1)
                Debug.Print UserForm11.ListBox1.List(z)
     
            If Mid(UserForm11.ListBox1.List(w), 1, InStr(1, UserForm11.ListBox1.List(w), " ") - 1) < Mid(UserForm11.ListBox1.List(z), 1, InStr(1, UserForm11.ListBox1.List(z), " ") - 1) Then
     
                         Temp = UserForm11.ListBox1.List(w)
                UserForm11.ListBox1.List(w) = UserForm11.ListBox1.List(z)
                UserForm11.ListBox1.List(z) = Temp
     
             End If
     
            If Mid(UserForm11.ListBox1.List(w), 1, InStr(1, UserForm11.ListBox1.List(w), " ") - 1) = Mid(UserForm11.ListBox1.List(z), 1, InStr(1, UserForm11.ListBox1.List(z), " ") - 1) Then
     
     
            If UserForm11.ListBox1.List(w) < UserForm11.ListBox1.List(z) Then
     
     
                Temp = UserForm11.ListBox1.List(w)
                UserForm11.ListBox1.List(w) = UserForm11.ListBox1.List(z)
                UserForm11.ListBox1.List(z) = Temp
                UserForm11.Repaint
     
                End If
     
            End If
        Next z
    Next w
     
     
     
     
    End Sub
    Quelle correction apporter à ce code???

    MERCI BEAUCOUP POUR TOUT COUP DE MAIN!!!!

  2. #2
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 056
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 056
    Points : 174
    Points
    174
    Par défaut
    Trouvé:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
     
    Sub TriFichRec()
     
            Dim w As Integer, z As Integer, Temp As String, UnVal As Long, DeuxVal As Long
     
    For w = 0 To UserForm11.ListBox1.ListCount - 1
        For z = w + 1 To UserForm11.ListBox1.ListCount - 1
     
                Debug.Print Mid(UserForm11.ListBox1.List(w), 1, InStr(1, UserForm11.ListBox1.List(w), " ") - 1)
                Debug.Print UserForm11.ListBox1.List(w)
                Debug.Print Mid(UserForm11.ListBox1.List(z), 1, InStr(1, UserForm11.ListBox1.List(z), " ") - 1)
                Debug.Print UserForm11.ListBox1.List(z)
     
     
    '            If Mid(UserForm11.ListBox1.List(w), 1, InStr(1, UserForm11.ListBox1.List(w), " ") - 1) = 319 Then
    '            Debug.Print "X"
    '            End If
     
                UnVal = CLng(Mid(UserForm11.ListBox1.List(w), 1, InStr(1, UserForm11.ListBox1.List(w), " ") - 1))
                DeuxVal = CLng(Mid(UserForm11.ListBox1.List(z), 1, InStr(1, UserForm11.ListBox1.List(z), " ") - 1))
     
            If UnVal < DeuxVal Then
     
                         Temp = UserForm11.ListBox1.List(w)
                UserForm11.ListBox1.List(w) = UserForm11.ListBox1.List(z)
                UserForm11.ListBox1.List(z) = Temp
     
             End If
     
            If UnVal = DeuxVal Then
     
     
            If UserForm11.ListBox1.List(w) < UserForm11.ListBox1.List(z) Then
     
     
                Temp = UserForm11.ListBox1.List(w)
                UserForm11.ListBox1.List(w) = UserForm11.ListBox1.List(z)
                UserForm11.ListBox1.List(z) = Temp
                UserForm11.Repaint
     
                End If
     
            End If
     
     
                   If UnVal > DeuxVal Then
     
                         Temp = UserForm11.ListBox1.List(z)
                UserForm11.ListBox1.List(z) = UserForm11.ListBox1.List(w)
                UserForm11.ListBox1.List(w) = Temp
     
             End If
     
            If UnVal = DeuxVal Then
     
     
            If UserForm11.ListBox1.List(w) > UserForm11.ListBox1.List(z) Then
     
     
                Temp = UserForm11.ListBox1.List(z)
                UserForm11.ListBox1.List(z) = UserForm11.ListBox1.List(w)
                UserForm11.ListBox1.List(w) = Temp
                UserForm11.Repaint
     
                End If
     
            End If
     
        Next z
    Next w
     
     
     
     
    End Sub

  3. #3
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 056
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 056
    Points : 174
    Points
    174
    Par défaut
    Oups, j'ai parlé un peu vite, le résultat est hyper aléatoire

Discussions similaires

  1. [Débutant] Affichage tri dans Listbox avec VB2010
    Par merymad69 dans le forum VB.NET
    Réponses: 4
    Dernier message: 01/02/2016, 10h29
  2. Tri dans listbox
    Par Chris171717 dans le forum Macros et VBA Excel
    Réponses: 16
    Dernier message: 05/03/2013, 10h22
  3. Tri dans listbox
    Par Basicnav dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 29/11/2009, 15h20
  4. Tri dans une listbox
    Par JLDpilot dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 27/04/2008, 15h28
  5. empecher le tri dans un listbox dynamiquement
    Par firejocker dans le forum MFC
    Réponses: 2
    Dernier message: 01/12/2005, 17h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo