Fonction Vlookup sur autre colonne de recherche et retour infos dans listbox

rom1z

XLDnaute Nouveau
Bonjour, je cherche à afficher les informations d'un tableau avec la fonction Vlookup mais ma donnée à chercher n'est pas dans la premiere colonne de mon tableau (colonne H) donc il me retourne une erreur et je ne sais pas comment contourner ce problème.
autre question concernant ce code, dans mon tableau la valeur cherchée est présente sur plusieurs lignes, je souhaiterai que la seule colonne dont les données changent (colonne C) de chaque ligne s'affichent les unes en dessous des autres dans un listbox (listbox2) comment faire ça ?
merci !


VB:
Private Sub CommandButton6_Click()
    If WorksheetFunction.CountIf(Sheets("EMPRUNT MATERIEL").Range("MISSRefMISSION"), Me.ComboBox2.Value) = 0 Then
    GoTo 5
    End If
    
    With Me
    
    '.ListBox2 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 8, 0)
    '.TextBox1 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 4, 0)
    .TextBox2 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 5, 0)
    .TextBox3 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 6, 0)
    .TextBox4 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 7, 0)
    .ComboBox1 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 1, 0)
    
    
    End With
5
End Sub
 

Hasco

XLDnaute Barbatruc
Repose en paix
Bonjour,

1 - enlever le With Me (inutile) et les point devant les .TextBox, le module de code sait très bien à qui appartiennent les objets.

Pour votre question, avec quelque chose d'approchant ceci si ce n'est pas cela :):
(Remplacer IndexColonneRecherche par l'index (relatif au tableau) de la colonne de recherche (1 à n))
VB:
Dim idx As Variant
    With Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT")
     'Application.Match = équivalent de la fonction de feuille EQUIV
     'Comme toutes les fonctions de feuille, peut retourner une erreur (xlCvError)
        idx = Application.Match(combobox2.Value, .Columns([B]IndexColonneRecherche[/B]), 0)
        '
        ' Si Trouvé
        If Not IsError(idx) Then
            'ListBox2 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 8, 0)
            'TextBox1 = Application.WorksheetFunction.VLookup(Me.ComboBox2, Sheets("EMPRUNT MATERIEL").Range("LISTEMPRUNT"), 4, 0)
            TextBox2 = .Cells(idx, 5)
            TextBox3 = .Cells(idx, 6)
            TextBox4 = .Cells(idx, 7)
            ComboBox1 = .Cells(idx, 1)
        End If
    End With

P.S. un fichier exemple eût été mieux. Si cela ne fonctionne pas, je ne ré-interviendrais pas.

Bonne soirée
 

rom1z

XLDnaute Nouveau
Bonjour Roblochon et merci pour ta réponse qui fonctionne, j'ai toutefois un soucis avec les formats de date, après avoir rappelé mes infos dans les textbox, à l'écriture dans le tableau excel inscrit les dates une fois sur deux en jj/mm/aa ou mm/jj/aa ??? très bizarre
je te joint le fichier anonymisé
je souhaiterais également qu'au rappel des infos (mission existante/afficher) le matériel déjà enregistré comme emprunté s'affiche aussi dans la listBox, mais je ne sais pas comment le coder.
merci !
 

Pièces jointes

  • MATOS ANON BETA.xlsm
    250.8 KB · Affichages: 22

Discussions similaires

Réponses
9
Affichages
812
Réponses
1
Affichages
775

Statistiques des forums

Discussions
311 725
Messages
2 081 940
Membres
101 845
dernier inscrit
annesof