在Range.Find VBA中使用Or函数
是否有可能把一个range.find和OR函数? 我有这个function,但我希望它寻找2值之一
With Worksheets("Term").Range("A:A") Set rng = .Find(What:=Worksheets("Term and Discipline").Range("K7"), _ After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) Application.Goto rng, True End With
在你提供的内容中,我尝试了一个OR语句,但是当我试图运行它时,Excel会生气
我想最接近的等价物将是并行(有效地)执行search,并使用MIN()
来selectfind的第一个单元格。
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
检查 – 使它变得有点混乱:
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
Range.Find
方法模拟与在Excel中键入CTRL + F时相同的窗口。 由于您无法在该窗口中同时search2+值,我不相信您可以通过VBA同时search2+值。 我想你不得不两次做这个方法。
如果我错了,我很想知道如何做到这一点; 这将是如此有用。 所以请随时certificate我错了:)
从我的testing中, Range.Find
方法不支持数组或一个以上的单元格范围。 所以那些都出来了。
此外,尝试使用OR
将无法正常工作,因为OR
检查TRUE / FALSE并返回TRUE / FALSE。
您需要循环遍历范围中的所有单元格,并在带有Or.
的If
语句中使用Like
函数或InStr
Or.