【问题标题】:Using an Or function within Range.Find VBA在 Range.Find VBA 中使用 Or 函数
【发布时间】:2013-07-26 19:59:38
【问题描述】:

是否可以在 range.find 中放置和 OR 函数?我有这个功能,但我希望它查找 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 时,Excel 对我很生气

【问题讨论】:

    标签: excel vba


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      Range.Find 方法模拟与您在 Excel 中键入 CTRL+F 时相同的窗口。由于您不能在该窗口中同时搜索 2+ 值,我不相信您可以通过 VBA 同时搜索 2+ 值。不幸的是,我认为您必须执行该方法两次。

      如果我错了,我很想知道该怎么做;它会很有用。所以请随时证明我错了:)

      根据我的测试,Range.Find 方法不支持数组或超过 1 个单元格的范围。所以这些都出来了。

      此外,尝试使用 OR 将无法正常工作,因为 OR 会检查 TRUE/FALSE 并返回 TRUE/FALSE。

      【讨论】:

        【解决方案3】:

        您需要遍历范围内的所有单元格,并在带有Or.If 语句中使用Like 函数或InStr

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-07
          • 1970-01-01
          • 1970-01-01
          • 2016-06-05
          • 1970-01-01
          • 2019-12-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多