Faire un Vlookup dans un autre fichier excel fermé

geo0258 Messages postés 20 Date d'inscription   Statut Membre Dernière intervention   -  
fabien25000 Messages postés 673 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour je n'arrive pas à écrire en VBA un Vlookup vers un autre fichier excel. Je ne sais pas comment intégrer l'adresse du fichier. J'ai beau chercher sur plein de forum je ne trouve pas.

Dans l'idée je voudrais remplir une colonne d'un fichier A avec le résultat de rechercheVs dans un autre fichier B et si possible sans avoir à ouvrir le fichier B. Donc le but c'est que l'utilisateur appuie sur un bouton, puis on lui demande de donner l'adresse du fichier (ex: file:///C:\Users\Desktop\Carnet%20de%20commandes.xlsm) et ensuite toute la colonne se remplie.

J'ai essayé un truc du genre:

Sub RechercheV_ADV()

ligne_fin = 1
range_recherche = inputbox ("rentrer l'adresse de votre fichier")

While Worksheets("feuil1").Cells(ligne_fin, 1) <> ""

Worksheets("feuil1").Cells(ligne_fin, 2) = WorksheetFunction.VLookup(Worksheets("feuil1").Cells(ligne_fin, 1), range_recherche.Worksheets("feuil1").Range("A:AA"), 19, 0)

ligne_fin = ligne_fin + 1

Wend

End Sub


Seulement ça ne marche pas. J'imagine bien que je n'ai pas fais ça comme il faut, j'ai essayé plein d'autre techniques mais je ne trouve pas comment l'écrire.

Merci d'avance.
A voir également:

1 réponse

fabien25000 Messages postés 673 Date d'inscription   Statut Membre Dernière intervention   59
 
Bonjour

J'ai écrit ce code il y a quleques temps, j'espère que tu pourra t'en inspirer :

 '********************************************************************************************************************************************
'----------------------------------------------------ALIMENTER 1 FICHIER AVEC DONNEES DE 2 AUTRES--------------------------------------------
'********************************************************************************************************************************************
    Dim i, J, DerLig, DerLigAnalyse As Long
    Dim DocXlA, DocXlF, DocXlJ As Excel.Workbook
    Dim AppXl As Excel.Application


Private Sub bValider_Click()
    Dim TmpMsg As Integer
    
    DerLig = DocXlF.Worksheets("TABLEAU").Range("a" & Rows.Count).End(xlUp).Row
    DerLigAnalyse = Worksheets("ANALYSE").Range("a" & Rows.Count).End(xlUp).Row
    
    Set DocXlA = ThisWorkbook
    
    For i = 2 To DerLig
        If lbRefAnalyse = DocXlF.Worksheets("TABLEAU").Range("a" & i) & " " & DocXlF.Worksheets("TABLEAU").Range("b" & i) Then
            DocXlF.Worksheets("TABLEAU").Range("A" & i & ":" & "V" & i).Copy
            With DocXlA.Worksheets("ANALYSE").Select
                Range("a" & DerLigAnalyse + 1).Select
                ActiveSheet.Paste
            End With
        End If
    Next i
    
    DerLig = DocXlJ.Worksheets("TABLEAU").Range("a" & Rows.Count).End(xlUp).Row
    For i = 2 To DerLig
        If lbRefAnalyse = DocXlJ.Worksheets("TABLEAU").Range("a" & i) & " " & DocXlJ.Worksheets("TABLEAU").Range("b" & i) Then
            For J = 1 To 20
                DocXlA.Worksheets("ANALYSE").Range(Chr(68 + J) & DerLigAnalyse + 1) = DocXlA.Worksheets("ANALYSE").Range(Chr(68 + J) & DerLigAnalyse + 1) + DocXlJ.Worksheets("TABLEAU").Range(Chr(68 + J) & i)
            Next J
            Exit For
        End If
    Next i
    TmpMsg = MsgBox("Voulez-vous analyser d'autre dossier?", vbYesNo + vbQuestion, "Continuer")
    If TmpMsg = vbYes Then
        lbRefAnalyse.Value = ""
    Else
        DocXlJ.Close
        DocXlF.Close
    End If
    
End Sub

Private Sub UserForm_Initialize()
    Dim i, DerLig As Integer
    Dim VarItem As String

    Set AppXl = CreateObject("Excel.Application")
    AppXl.Visible = False
    Set DocXlJ = AppXl.Workbooks.Open("chemin complet du fichier1.xlsm", ReadOnly:=False)
    Set DocXlF = AppXl.Workbooks.Open("chemin complet du fichier2.xlsm", ReadOnly:=False)
    
    lbRefAnalyse.Clear
    DerLig = DocXlF.Worksheets("TABLEAU").Range("a" & Rows.Count).End(xlUp).Row
    
    For i = 2 To DerLig
        VarItem = DocXlF.Worksheets("TABLEAU").Range("a" & i) & " " & DocXlF.Worksheets("TABLEAU").Range("b" & i)
        lbRefAnalyse.AddItem (VarItem)
    Next i
    
'********************************************************************************************************************************************
'-----------------------------------------------------------------FIN ALIMENTATION-----------------------------------------------------------
'********************************************************************************************************************************************

End Sub

0