Excel #VALUE!使用GetValue时出错

时间:2017-06-16 09:54:11

标签: excel vba 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!使用时如下图所示:

目标=&#39; C:\ Temp [pulltest.xlsx] Sheet1&#39;!$ A $ 1

参数C:\ Temp \             pulltest.xlsx             工作表Sheet1             A1

Function = getvalue(B3,B4,B5,B6)

我在Windows 7中使用Excel2010。

感谢任何帮助

1 个答案:

答案 0 :(得分: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

如果链接仍处于活动状态,您可以参考 Excel Tips from John Walkenbach: A VBA Function To Get A Value From A Closed File其中包括警告&#34; 注意:您不能在工作表公式中使用此功能。&#34;