我想最接近的等价物是并行(有效)执行搜索并MIN()
用于选择找到的第一个单元格。
Sub FindingNemor()
Dim rngFoo As Range
Dim rngBar As Range
Set rngFoo = Cells.Find(What:="foo", After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
Set rngBar = Cells.Find(What:="bar", After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not rngFoo Is Nothing And Not rngBar Is Nothing Then
Range("A1").Cells(Application.Min(rngFoo.Row, rngBar.Row)).Select
End If
End Sub
如果 rngFoo 或 rngBar 中只有一个是 Nothing,则需要额外检查。
添加了检查Nothing
-ness 使它有点混乱:
Sub FindingNemor()
Dim rngFoo As Range
Dim rngBar As Range
Set rngFoo = Cells.Find(What:="foo", After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
Set rngBar = Cells.Find(What:="bar", After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not rngFoo Is Nothing And Not rngBar Is Nothing Then
Range("A1").Cells(Application.Min(rngFoo.Row, rngBar.Row)).Select
ElseIf rngFoo Is Nothing Then
If rngBar Is Nothing Then
MsgBox "Neither found."
Else
Range("A1").Cells(rngBar.Row).Select
End If
Else
Range("A1").Cells(rngFoo.Row).Select
End If
End Sub