简体   繁体   中英

Run-Time Error 1004 When Using Vlookup with ExecuteExcel4Macro

How do you properly construct a VLOOKUP statement in Excel VBA when using the ExecuteExcel4Macro function in VBA?

I have a function that successfully looks up a value in another excel workbook without opening it using ExecuteExcel4Macro, but when I attempt to change the statement to a VLOOKUP statement I get a Run-time error 1004:

The function:

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

And it's usage in the subroutine:

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

I don't see any errors in how the VLOOKUP statement is constructed, does ExecuteExcel4Macro require a different type of statement or is there something else going on here?

Any help would be greatly appreciated, and if anyone happens to know if there is a manual for ExecuteExcel4Macro or any documentation of any real value that would also be helpful!

This is a possibility if it can be adopted:

Function:

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

Subroutine:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM