vba 如何检测VBA excel是否找到了一些东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1589939/
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
how to detect whether VBA excel found something?
提问by l--''''''---------''''''''''''
i am using this to in a macro to find stuff in my sheet:
我在宏中使用它来在我的工作表中查找内容:
Selection.Find(What:=email, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
how can i tell whether or not it found something?
我怎么知道它是否找到了什么?
回答by manji
Dim rng As Range
Set rng = Selection.Find(What:=email, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not rng Is Nothing Then 'when rng <> nothing means found something'
rng.Activate
End IF
回答by Ryan Shannon
Findreturns a Range object that will ave value Nothingif Whatis not found. From the help:
Find返回一个 Range 对象,Nothing如果What未找到,该对象将获取值。从帮助:
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
回答by NickSentowski
Selection.Find is like using Ctrl+F to find a value. You can then check against Activecell.Value to see if you got the desired result.
Selection.Find 就像使用 Ctrl+F 来查找一个值。然后,您可以检查 Activecell.Value 以查看是否获得了所需的结果。

