Excel公式可查找文本中的所有文本

时间:2019-03-11 13:29:20

标签: excel excel-formula find

我正在尝试在文本中查找文本,并将其显示在单元格中。

我能够找到这样的文本:

= MID(A2,FIND(“:”,A2)+1,FIND(“ /”,A2,FIND(“:”,A2)+1)-FIND(“:”,A2)-1)

但是,我有多个实例,其中该单元格中有多个字符串,且这些字符串都满足该条件。例如:

这只是一个示例:12345678 /,另一个示例 :23455663 /。

我的问题是,如何捕获所有以“:”开头并以“ /”结尾的值,而不仅仅是第一个实例?

此外,如果可能的话,如何在字符串之间用一行分隔每个值。

1 个答案:

答案 0 :(得分:1)

尝试以下用户定义功能:

Public Function FindAllText(s As String)
    Dim KaptureMode As Boolean, c As String
    Dim L As Long, i As Long, CH As String
    KaptureMode = False
    c = Chr(10)
    L = Len(s)
    For i = 1 To L
        CH = Mid(s, i, 1)
        If KaptureMode Then
            If CH = "/" Then
                KaptureMode = False
                FindAllText = FindAllText & c
            Else
                FindAllText = FindAllText & CH
            End If
        Else
            If CH = ":" Then
                KaptureMode = True
            End If
        End If
    Next i
    If Right(FindAllText, 1) = c Then FindAllText = Mid(FindAllText, 1, Len(FindAllText) - 1)
End Function

enter image description here

只需确保在启用Wrap的情况下使用UDF格式化单元格。

EDIT#1:

此版本将在字符串末尾附近检查不匹配的 /

Public Function FindAllText(s As String)
    Dim KaptureMode As Boolean, c As String
    Dim L As Long, i As Long, CH As String
    Dim Candidate As String

    Candidate = ""
    KaptureMode = False
    c = Chr(10)
    L = Len(s)
    For i = 1 To L
        CH = Mid(s, i, 1)
        If KaptureMode Then
            If CH = "/" Then
                KaptureMode = False
                FindAllText = FindAllText & Candidate & c
                Candidate = ""
            Else
                Candidate = Candidate & CH
            End If
        Else
            If CH = ":" Then
                KaptureMode = True
            End If
        End If
    Next i
    If Right(FindAllText, 1) = c Then FindAllText = Mid(FindAllText, 1, Len(FindAllText) - 1)
End Function

enter image description here

如您所见, abc 不会出现在输出中。