0

我尝试Vlookup通过ExecuteExcel4Macro. 由于我需要在不同的工作簿中找到很多值,因此在封闭的工作簿中进行操作是有意义的。

我使用了下面的代码,但如果我在我的 Excel 书上输入该函数,它就不起作用:

=GetVlookup("D:", "testfile.xls", "Sheet1", "C1:D8", 2, "CDM-01_10")

我可以保证我的D:名字上有一个文件,其中包含我在这里提到的范围内testfile.xls的 asheet1和内容。但结果是#value!

我想在一个函数中使用它,因为我想制作一个可以在多个工作簿中使用的价格计算插件,所以我宁愿没有在宏中引入它。

Public Function GetVlookup(path, file, sheet, ref, Col, vVal)
'   Retrieves a value from a closed workbook

    Dim arg As String

'   Make sure the file exists

If Right(path, 1) <> "\" Then path = path & "\"

If Dir(path & file) = "" Then
    GetVlookup = "File Not Found"
    Exit Function
End If

If IsNumeric(vVal) Then
  vVal = CDbl(vVal)
Else
  vVal = Chr(34) & vVal & Chr(34)
  End If

' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & Range(ref).Address(, , xlR1C1)
' Execute an XLM macro
GetVlookup = ExecuteExcel4Macro("Vlookup(" & vVal & "," & arg & "," & Col & ",0)")
End Function
4

0 回答 0