VBA发现函数获取错误424对象需要 [英] VBA find function getting error 424 object required

查看:474
本文介绍了VBA发现函数获取错误424对象需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码获取错误424对象需要

I have code getting error 424 object required

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


推荐答案

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

Find retrieves an range Object. So you either:

a)激活找到的范围

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

OR

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

b) Assign the found range to a variable

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.

注意:

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

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

这篇关于VBA发现函数获取错误424对象需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆