0

在 VBA 中使用 ExecuteExcel4Macro 函数时,如何在 Excel VBA 中正确构造 VLOOKUP 语句?

我有一个函数可以成功地在另一个 excel 工作簿中查找一个值,而无需使用 ExecuteExcel4Macro 打开它,但是当我尝试将语句更改为 VLOOKUP 语句时,我收到运行时错误 1004:

功能:

Public Function fGetValueTest(sFilePath, sFileName, sSourceSheet, sSourceCell, vVal, Col)
'Returns the value of a cell from a closed file [BD]

    'Declaring variables [BD]
    Dim sStringMacro As String
    Dim externalValue As Variant
    
    'Setting variables [BD]
    externalValue = ExecuteExcel4Macro("'" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & _
    Range("A1").Range(sSourceCell).Address(, , xlR1C1))
    
    'Exception error on file not found [BD]
    If Dir(sFilePath & sFileName) = "" Then
        fGetValueTest = "File Not Found!"
        Exit Function
    End If
    
    'If value of source cell is N/A [BD]:
    If Application.IsNA(externalValue) Then
        'Skip and move on [BD]
        fGetValueTest = "0"
    ElseIf IsError(externalValue) Then
        MsgBox "Error - Check fGetValue Function"
    Else
        'Creating macro variable [BD]
        sStringMacro = "'" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & _
        Range("A1").Range(sSourceCell).Address(, , xlR1C1)
        fGetValueTest = ExecuteExcel4Macro("Vlookup(" & vVal & "," & sStringMacro & "," & Col & ",0)")
        
    End If
    
End Function

它在子程序中的用法:

Sub TestGetValue()

    Dim sFileName As String
    Dim sFilePath As String
    Dim sSourceSheet As String
    Dim sSourceCell As String
    Dim sDestinationCell As String
    Dim sDestinationSheet As String
    Dim vVal As String
    Dim Col As String
    
    sFileName = "0306-0312 Margin Master.xlsx"
    sFilePath = "\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\"
    sSourceSheet = "Bakery"
    sDestinationSheet = "TestSheet"
    sSourceCell = "G10"
    sDestinationCell = "G10"
    vVal = "A10"
    Col = 3
    
    ThisWorkbook.Worksheets(sDestinationSheet).Range(sDestinationCell) = fGetValueTest(sFilePath, sFileName, sSourceSheet, sSourceCell, vVal, Col)

End Sub

我没有看到 VLOOKUP 语句的构造方式有任何错误,ExecuteExcel4Macro 是否需要不同类型的语句,还是这里发生了其他事情?

任何帮助将不胜感激,如果有人碰巧知道是否有 ExecuteExcel4Macro 的手册或任何具有实际价值的文档也将有所帮助!

4

1 回答 1

0

如果可以采用,这是一种可能性:

功能:

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

子程序:

Sub TestThingSub()
    Dim Varr As Variant
    Varr = GetVlookup("\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\", "0306-0312 Margin Master2.xlsx", "Sheet2", "A1:B26", 2, "HORSE")
    MsgBox Varr
End Sub
于 2017-05-24T21:32:37.693 回答