【问题标题】:VBA find function getting error 424 object required [duplicate]VBA查找函数需要错误424对象[重复]
【发布时间】:2020-10-20 16:25:58
【问题描述】:

我的代码得到错误 424 对象要求

lr = Range("O:O").Cells(Rows.Count, 1).End(xlUp).Row

For y = 0 To UBound(myVariable)
    a = myVariable(y)
    Range("O:O").Select
    Set objXL = GetObject(, "Excel.Application")
    Set z = Cells.Find(What:=a, After:=Range("O2"), LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    If z = "True" Then
        ActiveCell.Delete shift:=xlUp
    End If
    MsgBox z.Value
Next

【问题讨论】:

标签: excel vba


【解决方案1】:

Find 检索范围 Object。所以你要么:

a) 激活找到的范围

Cells.Find(What:=a, After:=Range("O2"), LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

b) 将找到的范围分配给变量

Set z = Cells.Find(What:=a, After:=Range("O2"), LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)      'No .Activate in here '

同时使用两者会产生错误。

注意:

小心点。如果.Find 没有找到匹配项,它将检索Nothing。在这种情况下,.Activate 将弹出错误消息。所以在这里使用一些错误处理。

【讨论】:

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