【问题标题】:Run-Time Error 1004 When Using Vlookup with ExecuteExcel4Macro将 Vlookup 与 ExecuteExcel4Macro 一起使用时出现运行时错误 1004
【发布时间】:2017-10-25 08:04:33
【问题描述】:

在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 的手册或任何具有实际价值的文档也将有所帮助!

【问题讨论】:

  • 您的代码似乎在单个单元格范围 '\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\[0306-0312 Margin Master.xlsx]Bakery'!G10 内查找字符串 "A10" 并返回该单个单元格的第三列。这会出错。
  • 对不起,不,它不会寻找字符串 "A10",我猜它会寻找单元格 A10 的内容,但我不知道它会是什么样的单元格 A10 for - 几乎可以肯定不是封闭工作簿中的那个,因为它需要作为'\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\[0306-0312 Margin Master.xlsx]Bakery'!R10C1 传递。 (你为什么不让自己的生活更轻松,打开另一个工作簿,做你需要的任何处理,然后再次关闭它?)
  • 谢谢你,我知道,让我的生活更轻松会很好。但是在这里的办公室里,我们有时会有数万行的工作簿,我正在尝试开发一种不需要 25 分钟就可以从关闭的工作簿中编译信息的解决方案。我采用了我最初尝试以不同方式采用的代码。如果我将其发布为可能的解决方案,我会在这里遇到麻烦吗?好吧,如果你感兴趣的话,我会做的。它以自己的方式工作,但我仍在努力适应它。感谢您离开 cmets!
  • 自我回答不一定是坏事,但最好只根据问题中的信息得出。您当前的问题反映了两个问题(1)传递的参数不正确和(2)正在搜索的字符串周围缺少引号。第一个问题只是“垃圾输入 - 垃圾输出”,如果不被告知正确的参数是什么就无法修复,但第二个问题可能对其他人有用。 ...
  • 我建议您编辑问题以显示更新后的代码 没有 If IsNumeric(vVal) Then ... End If bits 并简单地问“为什么这个 VLookup工作”。那么你的答案就完全有意义了,而且问题和答案对其他有类似问题的人很有用。 (不过,这只是我的观点——包括你自己在内的其他人可能不同意。)

标签: excel vba vlookup excel-4.0


【解决方案1】:

如果可以采用,这是有可能的:

功能:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多