Renvoi a une ligne a partir d'un double clic listbox

Résolu
Raphlight -  
 Raphlight -
Bonjour,

J'ai un fais une fonction recherche (textbox) et les résultats apparaissent dans différentes "listbox"

Jusque là tout vas bien.

mon code actuel :

Private Sub TextBox1_Change() 'www.blog-excel.com/creer-un-champ-de-recherche-vba
    
    Application.ScreenUpdating = False
 

    ListBox1.Clear 'Exemple 2 (ListBox)
    ListBox2.Clear
    ListBox3.Clear
    ListBox4.Clear
    ListBox5.Clear
    ListBox6.Clear
    ListBox7.Clear
    ListBox8.Clear
    
    If TextBox1 <> "" Then
        For ligne = 11 To 10000
            If Cells(ligne, 1) Like "*" & TextBox1 & "*" Then
                
                ListBox1.AddItem Cells(ligne, 1) 'Exemple 2 (ListBox)
                ListBox2.AddItem Cells(ligne, 2)
                ListBox3.AddItem Cells(ligne, 3)
                ListBox4.AddItem Cells(ligne, 4)
                ListBox5.AddItem Cells(ligne, 5)
                ListBox6.AddItem Cells(ligne, 6)
                ListBox7.AddItem Cells(ligne, 7)
                ListBox8.AddItem Cells(ligne, 8)
            End If
        Next
    End If
 
End Sub 


Ensuite je voudrais pouvoir cliquer sur une ligne de ma listbox et que cela me renvoi a la ligne correspondante dans ma base (juste dessous).
Et je ne vois pas comment faire...

J’espère avoir été assez clair (dans ma tête ce l'est toujours^^)

je vous remercie du dérangement

Cordialement.

4 réponses

  1. pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   2 773
     
    Bonjour,

    Erreur de compréhension...

    Désolé.
    En fait, il te faut stocker les numéros des lignes dans une seconde colonne cachée d'une listbox.

    Avant, j'arrivais jamais à finir mes phrases... mais maintenant je
    0
  2. f894009 Messages postés 17417 Date d'inscription   Statut Membre Dernière intervention   1 717
     
    Bonjour,

    cliquer sur une ligne de ma listbox Y a 8 TextBoxs, laquelle allez-vous choisir ???????????????????????????????????
    0
    1. Raphlight
       
      la une, après je recopierais le code pour appliquer aux autres liste si besoin.
      0
  3. pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   2 773
     
    Re

    Quelque chose comme ceci :

    Private Sub TextBox1_Change() 'www.blog-excel.com/creer-un-champ-de-recherche-vba
        
        Application.ScreenUpdating = False
     
    
        ListBox1.Clear 'Exemple 2 (ListBox)
        ListBox1.ColumnCount = 2
        ListBox1.ColumnWidths = ListBox1.Width & ";0"
        ListBox2.Clear
        ListBox2.ColumnCount = 2
        ListBox2.ColumnWidths = ListBox2.Width & ";0"
        ListBox3.Clear
        ListBox3.ColumnCount = 2
        ListBox3.ColumnWidths = ListBox3.Width & ";0"
        ListBox4.Clear
        ListBox4.ColumnCount = 2
        ListBox4.ColumnWidths = ListBox4.Width & ";0"
        ListBox5.Clear
        ListBox5.ColumnCount = 2
        ListBox5.ColumnWidths = ListBox5.Width & ";0"
        ListBox6.Clear
        ListBox6.ColumnCount = 2
        ListBox6.ColumnWidths = ListBox6.Width & ";0"
        ListBox7.Clear
        ListBox7.ColumnCount = 2
        ListBox7.ColumnWidths = ListBox7.Width & ";0"
        ListBox8.Clear
        ListBox8.ColumnCount = 2
        ListBox8.ColumnWidths = ListBox8.Width & ";0"
        
        If TextBox1 <> "" Then
            For ligne = 11 To 10000
                If Cells(ligne, 1) Like "*" & TextBox1 & "*" Then
                    
                    ListBox1.AddItem Cells(ligne, 1) 'Exemple 2 (ListBox)
                    ListBox1.List(ListBox1.ListCount - 1, 1) = ligne
                    ListBox2.AddItem Cells(ligne, 2)
                    ListBox2.List(ListBox2.ListCount - 1, 1) = ligne
                    ListBox3.AddItem Cells(ligne, 3)
                    ListBox3.List(ListBox3.ListCount - 1, 1) = ligne
                    ListBox4.AddItem Cells(ligne, 4)
                    ListBox4.List(ListBox4.ListCount - 1, 1) = ligne
                    ListBox5.AddItem Cells(ligne, 5)
                    ListBox5.List(ListBox5.ListCount - 1, 1) = ligne
                    ListBox6.AddItem Cells(ligne, 6)
                    ListBox6.List(ListBox6.ListCount - 1, 1) = ligne
                    ListBox7.AddItem Cells(ligne, 7)
                    ListBox7.List(ListBox7.ListCount - 1, 1) = ligne
                    ListBox8.AddItem Cells(ligne, 8)
                    ListBox8.List(ListBox8.ListCount - 1, 1) = ligne
                End If
            Next
        End If
     
    End Sub 
    Private Sub ListBox1_Click()
    MsgBox ListBox1.List(ListBox1.ListIndex, 1)
    End Sub
    Private Sub ListBox2_Click()
    MsgBox ListBox2.List(ListBox2.ListIndex, 1)
    End Sub
    Private Sub ListBox3_Click()
    MsgBox ListBox3.List(ListBox3.ListIndex, 1)
    End Sub
    Private Sub ListBox4_Click()
    MsgBox ListBox4.List(ListBox4.ListIndex, 1)
    End Sub
    Private Sub ListBox5_Click()
    MsgBox ListBox5.List(ListBox5.ListIndex, 1)
    End Sub
    Private Sub ListBox6_Click()
    MsgBox ListBox6.List(ListBox6.ListIndex, 1)
    End Sub
    Private Sub ListBox7_Click()
    MsgBox ListBox7.List(ListBox7.ListIndex, 1)
    End Sub
    Private Sub ListBox8_Click()
    MsgBox ListBox8.List(ListBox8.ListIndex, 1)
    End Sub
     

    0
    1. Raphlight
       
      après le clic cela m'annonce le numéro de la ligne correspondante.

      Et je voudrais si possible m'y être dirigé directement.

      Merci pour votre temps
      0
      1. pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   2 773 > Raphlight
         
        Désolé, j'ai cru que vous pourriez adapter seul.
        Dans le code donné plus haut, remplacer tous les :
        MsgBox ListBox7.List(ListBox7.ListIndex, 1)

        Par :
        Range("A" & ListBox7.List(ListBox7.ListIndex, 1)).Select


        En adaptant, bien sur, le numéro de la listbox...
        0
    2. Raphlight
       
      MERCI !

      Désolé je touche vraiment le basique et apprend sur le tas.

      Pour la suite je vais me débrouiller ;)

      Bonne journée tout le monde.
      0
    3. Raphlight
       
      Pour la ligne que tu a rajouté :

      ListBox1.List(ListBox1.ListCount - 1, 1) = ligne
      Cela fait quoi de plus que avant ?

      Ainsi que ces deux ligne suplementaire :

      ListBox1.ColumnCount = 2
      ListBox1.ColumnWidths = ListBox1.Width & ";0"

      J’essaye de comprendre plutôt que tout recopier bêtement ^^
      0
    4. Raphlight
       
      Avant il me trouvait les résultats avec ou sans majuscule. plus maintenant.
      0
  4. pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   2 773
     
    Avant il me trouvait les résultats avec ou sans majuscule. plus maintenant.
    Les lignes de cde que j'ai ajouté ne changent rien à ta recherche...
    Tout se fait là :
    If Cells(ligne, 1) Like "*" & TextBox1 & "*" Then

    Pour trouver indiféremment avec ou sans majuscules :
    If UCase(Cells(ligne, 1)) Like "*" & UCase(TextBox1) & "*" Then


    Explications :
    ListBox1.ColumnCount = 2 
    ==> Paramètre la listBox1 en multicolonne avec 2 colonnes
    ListBox1.ColumnWidths = ListBox1.Width & ";0" 
    ==> règle la largeur des deux colonnes :
    ====> Largeur Colonne 1 = ListBox1.Width (soit la largeur de la listbox)
    ====> Largeur colonne 2 = 0 (colonne invisible)
    ListBox1.List(ListBox1.ListCount - 1, 1) = ligne 
    ==> place le numéro de la ligne dans la colonne 2 de la listbox (colonne 2 = invisible)

    0
    1. Raphlight
       
      Merci pour tout.
      0