VBA查找函数获取所需的错误424对象

时间:2012-11-23 08:39:29

标签: excel-vba vba excel

我有代码获取错误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

1 个答案:

答案 0 :(得分:1)

查找检索范围对象。所以你要么:

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).Activate      'No .Activate in here '

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

注意:

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