1

我正在尝试使用代码

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。

感谢任何帮助

4

1 回答 1

0

您的问题是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 函数 ,其中包含警告“注意:您不能在工作表公式中使用此函数。

于 2017-06-16T12:27:33.523 回答