Vba Excel do vlookup from a closed file

梦想的初衷 提交于 2019-11-26 07:49:48

问题


I would like to do a vlookup from a close file to an active open file, The path of the closed file will be given as a variant

Lets say mypath = \"C:\\list\\....\"

I don\'t know if it is better to set the path only or the full-path (path+filename)

Could u please help ?


回答1:


You will need the full path.

Example

Option Explicit
Public Sub Example()
    Dim Path As String

    Path = "C:\Temp\"

    With ThisWorkbook.Sheets("Sheet1")
        .Range("B1").Formula = "=VLOOKUP(A1,'" & Path & "[Book1.xlsx]Sheet2'!A:J,3,FALSE)"
    End With
End Sub

VLOOKUP function




回答2:


As long as @0m3r's solution answers the question here is a small contribution to anyone who is looking to reference a closed file located in Desktop.

Even not specific for one user, works on any other user as well.

Option Explicit
Public Sub Example()
    Dim yourdesktopaddress As String 

    yourdesktopaddress = CreateObject("WScript.Shell").specialfolders("Desktop")

'or if you would like to reference another folder located in Desktop (i.e: SpecificFolderOnTheDesktop)
    'Dim folderinthedesktopaddress as String
    'folderinthedesktopaddress = CreateObject("WScript.Shell").specialfolders("Desktop") & "\SpecificFolderOnTheDesktop"

    With ThisWorkbook.Sheets("Sheet1")
        .Range("B1").Formula = "=VLOOKUP(A1,'" & yourdesktopaddress & "[Book1.xlsx]Sheet2'!A:J,3,FALSE)"
    End With
End Sub


来源:https://stackoverflow.com/questions/42799757/vba-excel-do-vlookup-from-a-closed-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!