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
VBA find function getting error 424 object required
提问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 .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.
小心点。如果.Find
没有找到匹配项,它将检索Nothing
. 在这种情况下,.Activate
将弹出错误消息。所以在这里使用一些错误处理。