vba VBA查找函数获取错误424对象需要

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13525477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:34:59  来源:igfitidea点击:

VBA find function getting error 424 object required

excel-vbavbaexcel

提问by mahesh patil

I have code getting error 424 object required

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

回答by CaBieberach

Find retrieves an range Object. So you either:

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

a) Activate the found range

a) 激活找到的范围

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

OR

或者

b) Assign the found range to a variable

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 '

Using both at the same time will produce an error.

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

NOTE:

笔记:

Be carefull. If .Finddoes NOT find a match, it will retrieve Nothing. In such a case the .Activatewill pop an error msg. So use some error handling in here.

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