Vba Excel从关闭的文件中执行vlookup

时间:2017-03-15 01:48:20

标签: excel vba excel-vba excel-formula vlookup

我想从关闭文件到活动打开文件执行vlookup,关闭文件的路径将作为变体提供

让我们说mypath = "C:\list\...."

我不知道最好只设置路径还是全路径(路径+文件名)

你可以帮忙吗?

2 个答案:

答案 0 :(得分:4)

您需要完整的路径。

实施例

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

答案 1 :(得分:2)

只要@ 0m3r的解决方案回答这里的问题,对于想要引用位于桌面中的已关闭文件的任何人来说都是一个小小的贡献。

  

即使不是针对一个用户,也适用于任何其他用户。

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