【问题标题】:Vba Excel Vlookup Search value is StringVba Excel Vlookup 搜索值为字符串
【发布时间】:2017-11-08 12:49:30
【问题描述】:

下面的代码不起作用

运行时错误 1004

VlookSrc(VBA 中的 Dim As String)是我们在 Range("KV5:KW105673") 中寻找的值。它是不同单元格的组合,具体取决于 c 和 d,以及介于两者之间的字符串 /DIR/。 在本地窗口中,可以看到 Excel 识别出正确的字符串。 我们使用IfError 函数,因为不会总是找到值。 运行错误1004是worksheetfunction自带的。

    If Cells(3, d) = Cells(c, 33) Then
        VlookSrc = Cells(c, 48).Value & "/DIR/" & Cells(4, d).Value
       Cells(c, d) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(VlookSrc, Range(Cells(5, 308), Cells(105673, 309)), 2, False), 0)
    Else
        Cells(c, d) = 0
    End If

【问题讨论】:

  • 现在我们发现问题出在日期的单元格格式上。但是我们仍然遇到找不到值的查找值的问题。如何使用 iferror 工作表函数?

标签: vba excel


【解决方案1】:

您不能像在 VBA 中那样使用IfError(),而是:

Sub luxation()
    Dim v
    With Application.WorksheetFunction
        v = 0
        On Error Resume Next
            Cells(c, d) = .VLookup(VlookSrc, Range(Cells(5, 308), Cells(105673, 309)), 2, False)
        On Error GoTo 0
    End With
End Sub

【讨论】:

    【解决方案2】:

    删除WorksheetFunction,因为它总是会引发运行时错误(网上有很多关于此的内容),而Application 允许测试错误。

    Sub x()
    
    If Cells(3, d) = Cells(c, 33) Then
        VlookSrc = Cells(c, 48).Value & "/DIR/" & Cells(4, d).Value
        Cells(c, d) = Application.IfError(Application.VLookup(VlookSrc, Range(Cells(5, 308), Cells(105673, 309)), 2, False), 0)
    Else
        Cells(c, d) = 0
    End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多