gpt4 book ai didi

excel - 在 Range.Find VBA 中使用 Or 函数

转载 作者:行者123 更新时间:2023-12-04 21:25:22 24 4
gpt4 key购买 nike

是否可以在 range.find 中放置和 OR 函数?我有这个功能,但我希望它寻找两个值中的任何一个

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 时,Excel 对我很生气

最佳答案

我想最接近的等价物是并行(有效)执行搜索并使用 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

关于excel - 在 Range.Find VBA 中使用 Or 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17890004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com