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 :

Récupérer le resultat de ma listbox dans une variable pour nouvelle recherche


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    47
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 47
    Par défaut Récupérer le resultat de ma listbox dans une variable pour nouvelle recherche
    bonjour,
    j'ai recupéré un petit programme sur le net et je suis coincée à un niveau. j'ai une listbox qui me renvoie un resultat suivant le terme recherché dans un textbox1 . j'ai un deuxième textbox dans lequel je rentre un deuxième mot à rechercher dans la listbox affichée. Comment affecter le resultat de la listbox à une variable pour pouvoir refaire une recherche dessus et afficher le resultat dans la même listbox. En fait c'est un tri à deux niveaux.
    ci-dessous le code:
    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    Option Explicit
    Dim NbreCol As Long
     
    Private Sub CommandButton1_Click()
    Unload UserForm1
    End Sub
     
    Private Sub TextBox1_Change()
    Dim S As String, i As Long, Lmax As Long, Nbr As Long, k As Long
    Dim L, V, LigneOK, m, A As Long           '<<< ligne modifiée
    NbreCol = 13     '<---- Nombre de colonnes à prendre en compte
     
        If TextBox1 = "" Then
            init
        Else
        UserForm1.TextBox2 = ""
            ListBox1.Clear
            Lmax = Range("A" & Rows.Count).End(xlUp).Row
            V = Range(Range("A2"), Cells(Lmax, NbreCol)).Value
            ReDim LigneOK(1 To Lmax - 1)            '<<< ligne modifiée
     
            '-------------------------------------------------------------------------------------'
            ' si on ne veut qu'une présence du texte saisi en début de cellule, conserver l'instruction suiv.
            'S = LCase(TextBox1) & "*"
            ' si le texte saisi peut se trouver n'importe où dans les cellules, conserver l'instructiion suiv.
            'S = S = "*" & LCase(TextBox1) & "*"
            '-------------------------------------------------------------------------------------'
            S = "*" & LCase(TextBox1) & "*"
            For i = 1 To Lmax - 1
                For m = 1 To NbreCol                        '<<< ligne modifiée
                    LigneOK(i) = False                          '<<< ligne modifiée
                    If LCase(V(i, m)) Like S Then           '<<< ligne modifiée
                        LigneOK(i) = True                       '<<< ligne modifiée
                        Nbr = Nbr + 1                             '<<< ligne modifiée
                        Exit For                                        '<<< ligne modifiée
                    End If                                               '<<< ligne modifiée
                Next m                                                 '<<< ligne modifiée
            Next i
            If Nbr = 0 Then Exit Sub
                ReDim L(0 To Nbr - 1, 0 To NbreCol - 1)
                Nbr = -1
                For i = 1 To Lmax - 1
                    If LigneOK(i) Then                            '<<< ligne modifiée
                        Nbr = Nbr + 1
                        For k = 0 To NbreCol - 1
                            L(Nbr, k) = V(i, k + 1)
                        Next k
                    End If
                Next i
                ListBox1.List = L
                A = ListBox1.ListCount
        End If
        Label1 = ListBox1.ListCount
    End Sub
    Private Sub TextBox2_Change()
    Dim S As String, j As Long, Lmax As Long, Nbr As Long, k As Long
    Dim D, V, U, LigneOK, m As Long          '<<< ligne modifiée
    Dim L1 As Range
    NbreCol = 13     '<---- Nombre de colonnes à prendre en compte
     
        If TextBox2 = "" Then
     
        Else
        j = 1
        'Set L1 = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp).Offset(, 2))
           U = ListBox1.List
           'V.Value = ListBox1.List
            Lmax = ListBox1.ListCount
            V = Range(Range("a2"), Cells(Lmax, NbreCol)).Value
            ReDim LigneOK(1 To Lmax)            '<<< ligne modifiée
     
            S = "*" & LCase(TextBox2) & "*"
     
     
            For j = 1 To Lmax - 1
                For m = 1 To NbreCol                        '<<< ligne modifiée
                    LigneOK(j) = False                          '<<< ligne modifiée
                    If LCase(V(j, m)) Like S Then           '<<< ligne modifiée
                        LigneOK(j) = True                       '<<< ligne modifiée
                        Nbr = Nbr + 1                             '<<< ligne modifiée
                        Exit For                                        '<<< ligne modifiée
                    End If                                               '<<< ligne modifiée
                Next m                                                 '<<< ligne modifiée
            Next j
            If Nbr = 0 Then Exit Sub
                ReDim D(0 To Nbr - 1, 0 To NbreCol - 1)
                Nbr = -1
                For j = 1 To Lmax - 1
                    If LigneOK(j) Then                            '<<< ligne modifiée
                        Nbr = Nbr + 1
                        For k = 0 To NbreCol - 1
                            D(Nbr, k) = V(j, k + 1)
                        Next k
                    End If
                Next j
                ListBox1.List = D
                End If
        Label1 = ListBox1.ListCount
    End Sub
     
    Sub init()
    Dim xRg As Range
        Set xRg = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp).Offset(, 2))
        'ListBox1.ColumnHeads = False
        ListBox1.List = xRg.Value
        ListBox1.Activate
        TextBox1.Activate
    End Sub
     
     
     
    Private Sub UserForm_Click()
     
    End Sub
    merci

  2. #2
    Inactif  
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    1 733
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 1 733
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    variable=listbox1.value

  3. #3
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    47
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 47
    Par défaut
    Je te remercie pour ta prompte réaction nouveauVBA mais j'ai l'erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    If LCase(V(j, m)) Like S Then
    au niveau de cette ligne dès que je rentre la première lettre dans le deuxième texbox.

  4. #4
    Inactif  
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    1 733
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 1 733
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    V = Range(Range("a2"), Cells(Lmax, NbreCol)).Value
    Tu n'as que du texte dans ton range V ?

    Parce que Lcase n'accepte que des strings ou des char, si c'est du numérique ça va planter...

    Devrait résoudre:
    Par contre tu poses une question et ton problème n'a rien à voir avec ta question... pas super utile

  5. #5
    Membre actif
    Inscrit en
    Mai 2008
    Messages
    47
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 47
    Par défaut
    ça marche très bien sur la première recherche celle avec le textbox1 avec LCASE qui prend chiffre et lettre. mais cette recherche ne fonctionne pas avec la deuxième recherche qui est basée sur le resultat de la première.
    malheureusement j'arrive pas à ajouter ce fichier car l'extensiion n'est pas acceptée ici. svp essayer d'executer ce code en créant deux text box et une listbox et une petite base dans une feuille excel
    je vous renvoie le code qui marche mieux mais la deuxième recherche n'est tjrs pas bonne. un autre methode serait que je copie la listbox dans la deuxième mais même là ça ne fonctionne pas.
    ci-joint le code
    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    Option Explicit
    Dim NbreCol As Long
     
    Private Sub CommandButton1_Click()
    Unload UserForm1
    End Sub
     
    Private Sub TextBox1_Change()
    Dim S As String, i As Long, Lmax As Long, Nbr As Long, k As Long
    Dim L, V, LigneOK, m, A As Long           '<<< ligne modifiée
    NbreCol = 13     '<---- Nombre de colonnes à prendre en compte
     
        If TextBox1 = "" Then
            init
        Else
        UserForm1.TextBox2 = ""
            ListBox1.Clear
            Lmax = Range("A" & Rows.Count).End(xlUp).Row
            V = Range(Range("A2"), Cells(Lmax, NbreCol)).Value
            ReDim LigneOK(1 To Lmax - 1)            '<<< ligne modifiée
     
            '-------------------------------------------------------------------------------------'
            ' si on ne veut qu'une présence du texte saisi en début de cellule, conserver l'instruction suiv.
            'S = LCase(TextBox1) & "*"
            ' si le texte saisi peut se trouver n'importe où dans les cellules, conserver l'instructiion suiv.
            'S = S = "*" & LCase(TextBox1) & "*"
            '-------------------------------------------------------------------------------------'
            S = "*" & LCase(TextBox1) & "*"
            For i = 1 To Lmax - 1
                For m = 1 To NbreCol                        '<<< ligne modifiée
                    LigneOK(i) = False                          '<<< ligne modifiée
                    If LCase(V(i, m)) Like S Then           '<<< ligne modifiée
                        LigneOK(i) = True                       '<<< ligne modifiée
                        Nbr = Nbr + 1                             '<<< ligne modifiée
                        Exit For                                        '<<< ligne modifiée
                    End If                                               '<<< ligne modifiée
                Next m                                                 '<<< ligne modifiée
            Next i
            If Nbr = 0 Then Exit Sub
                ReDim L(0 To Nbr - 1, 0 To NbreCol - 1)
                Nbr = -1
                For i = 1 To Lmax - 1
                    If LigneOK(i) Then                            '<<< ligne modifiée
                        Nbr = Nbr + 1
                        For k = 0 To NbreCol - 1
                            L(Nbr, k) = V(i, k + 1)
                        Next k
                    End If
                Next i
                ListBox1.List = L
                A = ListBox1.ListCount
        End If
        Label1 = ListBox1.ListCount
    End Sub
    Private Sub TextBox2_Change()
    Dim S As String, j As Long, Lma As Long, Nbr As Long, k As Long
    Dim D, V, U, LigneOK, m As Long          '<<< ligne modifiée
    Dim L1 As Range
    NbreCol = 13     '<---- Nombre de colonnes à prendre en compte
     
        If TextBox2 = "" Then
     
        Else
        j = 1
        Lma = 0
        Nbr = 0
        k = 0
        D = 0
        V = 0
        U = 0
        m = 0
        LigneOK = 0
     
            Lma = ListBox1.ListCount
            'Feuil2.Range(Range("A2"), Cells(Lma, NbreCol)).Value = ListBox1.Value
            Set L1 = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp).Offset(, 2))
            'Set U = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp).Offset(, 2))
             L1.Value = ListBox1.List
             U = Range(Range("A2"), Cells(Lma, NbreCol)).Value
            ReDim LigneOK(1 To Lma)            '<<< ligne modifiée
     
            S = "*" & LCase(TextBox2) & "*"
     
     
            For j = 1 To Lma - 1
                For m = 1 To NbreCol                        '<<< ligne modifiée
                    LigneOK(j) = False                          '<<< ligne modifiée
                    If LCase(U(j, m)) Like S Then           '<<< ligne modifiée
                        LigneOK(j) = True                       '<<< ligne modifiée
                        Nbr = Nbr + 1                             '<<< ligne modifiée
                        Exit For                                        '<<< ligne modifiée
                    End If                                               '<<< ligne modifiée
                Next m                                                 '<<< ligne modifiée
            Next j
            If Nbr = 0 Then Exit Sub
                ReDim D(0 To Nbr - 1, 0 To NbreCol - 1)
                Nbr = -1
                For j = 1 To Lma - 1
                    If LigneOK(j) Then                            '<<< ligne modifiée
                        Nbr = Nbr + 1
                        For k = 0 To NbreCol - 1
                            D(Nbr, k) = U(j, k + 1)
                        Next k
                    End If
                Next j
                ListBox1.List = D
                End If
        Label1 = ListBox1.ListCount
    End Sub
     
    Sub init()
    Dim xRg As Range
        Set xRg = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp).Offset(, 2))
        'ListBox1.ColumnHeads = False
        ListBox1.List = xRg.Value
        ListBox1.Activate
        TextBox1.Activate
    End Sub
     
    Private Sub UserForm_Click()
     
    End Sub

  6. #6
    Inactif  
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    1 733
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 1 733
    Par défaut
    tu connais le format ZIP ? Explique ce que tu veux clairement et joint ton fichier

Discussions similaires

  1. affecter le resultat d'un select dans une variable
    Par ricoree78 dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 07/06/2009, 14h48
  2. Récupérer le contenu de la requête dans une variable
    Par cgonzalez dans le forum Langage
    Réponses: 1
    Dernier message: 06/10/2007, 11h44
  3. Resultat D'1 Formule Dans Une Variable
    Par damsmut dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 14/12/2006, 15h07
  4. récupérer le résultat d'un SELECT dans une variable ?
    Par budhax dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 06/06/2006, 10h24
  5. utiliser le resultat d'un SUM dans une variable
    Par Qapoka dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 16/05/2006, 15h47

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