寻找多个术语(按优先级排序)

时间:2016-06-22 15:56:27

标签: vba excel-vba find excel

是否可以使用find方法搜索备份选项?

现在我的代码:

 Set foundCell = Cells.Find(What:="RCP 1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)
        If Not foundCell Is Nothing Then
            foundCell.Activate
            foundCell.Rows("1:1").EntireRow.Select
            Selection.Copy
            Range("A" & (PLcount + 8)).Select
            ActiveSheet.Paste
        Else
            Set foundCell = Cells.Find(What:="RCP- 1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)
                If Not foundCell Is Nothing Then
                    foundCell.Activate
                    foundCell.Rows("1:1").EntireRow.Select
                    Selection.Copy
                    Range("A" & (PLcount + 8)).Select
                    ActiveSheet.Paste
                End If
        End If

我希望能够做到类似下面的事情。注意.Find(What:=)

之后的文本
 Set foundCell = Cells.Find(What:="RCP 1" "RCP- 1" "RCP  1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)
        If Not foundCell Is Nothing Then
            foundCell.Activate
            foundCell.Rows("1:1").EntireRow.Select
            Selection.Copy
            Range("A" & (PLcount + 8)).Select
            ActiveSheet.Paste
        End If

如果第一项是第一优先,第二项是第二优先,第三项是第三优先,等等。

1 个答案:

答案 0 :(得分:3)

编辑 - Find()中对通配符的支持有限 - 您可能不会将其归类为"正则表达式"功能性:

* - zero or more characters
? - single character
~ - escapes * or ? if you want to find those literal characters

或者可以将Find放入单独的函数中:

Sub Tester()

    Dim foundCell, PLCount As Long

    PLCount = 3
    Set foundCell = FindFirst(Cells, Array("RCP 1", "RCP- 1"))

    If Not foundCell Is Nothing Then
        'no need for any select/activate
        foundCell.EntireRow.Copy Destination:=Range("A" & (PLCount + 8))
    End If

End Sub

'return the first match to a value in the array "arrWhat"
'   Returns Nothing if no match
Function FindFirst(rngWhere, arrWhat) As Range
    Dim v, f As Range
    For Each v In arrWhat
        Set f = rngWhere.Find(what:=v, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
        If Not f Is Nothing Then Exit For
    Next v
    Set FindFirst = f
End Function