【问题标题】:VBA Vlookup "application-defined or object-defined" error in excelexcel中的VBA Vlookup“应用程序定义或对象定义”错误
【发布时间】:2020-11-04 13:14:22
【问题描述】:

我尝试使用 excel VBA vlookup,但在第 4 行出现“应用程序定义或对象定义”错误。

Dim SearchRange As Range
Dim FindRow As Range
Set SearchRange = Sheet2.Range("A4", Sheet2.Range("A65536").End(xlUp))

Set FindRow = SearchRange.Find("Car 4", LookIn:=xlValues, lookat:=xlWhole)

【问题讨论】:

  • 实际使用Find不是VLOOKUP!查看WorksheetFunction.VLookup method
  • 这是您的确切代码吗?它没有任何明显的问题会导致您描述的错误。

标签: excel vba excel-formula office365 vlookup


【解决方案1】:

检查您的搜索范围SearchRange。例如,通过使用以下语句之一:

  • Debug.Print SearchRange.Address
  • MsgBox SearchRange.Address

进一步改进:

  1. 永远不要将A65536 硬编码为最后一个单元格。总是像这样使用它

    Sheet2.Range("A4", Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp))
    

    因为Rows.Count 为您提供了真实的行数(较新的 Excel 版本超过了65536 行数)。

  2. 请注意,Range.Find method 的文档说:

    每次使用此方法时都会保存LookInLookAtSearchOrderMatchByte 的设置。

    因此,如果您没有定义所有提到的 4 个参数,VBA 将使用最后使用的任何参数,无论是通过 VBA 还是用户界面。由于用户界面不在您的控制范围内,如果您不定义它们all,您将得到随机结果。

    Set FindRow = SearchRange.Find(What:="Car 4", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchByte:=False)
    
  3. 在使用FindRow之前一定要检查是否发现了什么东西

    Dim SearchRange As Range
    Set SearchRange = Sheet2.Range("A4", Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp))
    
    Dim FindRow As Range
    Set FindRow = SearchRange.Find(What:="Car 4", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, MatchByte:=False)
    
    'check if something was found
    If Not FindRow Is Nothing Then
        'successful
        MsgBox "found in row " & FindRow.Row
    Else
        MsgBox "'Car 4' was not found in range " & SearchRange.Address
    End If
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多