Excel # ЗНАЧЕНИЕ! ошибка при использовании 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

Но получите # ЗНАЧЕНИЕ! при использовании, как показано ниже:

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

Параметры C:\Temp\ pulltest.xlsx Sheet1 A1

Функция =getvalue(B3,B4,B5,B6)

Я использую Excel2010 в Windows 7.

Благодарен за любую помощь

1 ответ

Ваша проблема в том, что 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 от Джона Уокенбаха: функция VBA для получения значения из закрытого файла с предупреждением "Примечание. Эту функцию нельзя использовать в формуле рабочего листа".

Другие вопросы по тегам