Excel #VALUE! 使用GetValue时出错

我正在尝试使用该代码

Private Function GetValue(path, file, sheet, ref) ' 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 GetValue = "File Not Found" Exit Function End If ' Create the argument arg = "'" & path & "[" & file & "]" & sheet & "'!" & _ Range(ref).Range("A1").Address(, , xlR1C1) ' Execute an XLM macro GetValue = ExecuteExcel4Macro(arg) End Function 

但收到#VALUE! 使用时如下图所示:

目标='C:\ Temp [pulltest.xlsx] Sheet1'!$ A $ 1

参数C:\ Temp \ pulltest.xlsx Sheet1 A1

函数= getvalue(B3,B4,B5,B6)

我在Windows 7中使用Excel2010。

感谢任何帮助

您的问题是无法从工作表单元格中调用GetValue函数。

所以下面的工作:

 Option Explicit Sub GetVal() Dim path As String, file As String, sheet As String, ref As String path = [a7] file = [a8] sheet = [a9] ref = [a10] [a11] = GetValue(path, file, sheet, ref) End Sub Private Function GetValue(path, file, sheet, ref) 'Retrieves a value from a closed workbook Dim Arg 'Make sure the file exists If Right(path, 1) <> "\" Then path = path & "\" If Dir(path & file) = "" Then GetValue = "File not Found" Exit Function End If 'Create the argument Arg = "'" & path & "[" & file & "]" & CStr(sheet) & "'!" & Range(ref).Range("A1").Address(, , xlR1C1) GetValue = ExecuteExcel4Macro(Arg) End Function 

如果链接仍处于活动状态,则可以参考John Walkenbach提供的Excel提示:一个VBA函数从一个已closures的文件中获取一个值 ,其中包含注意事项注意:不能在工作表公式中使用此函数 ”。