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

Excel公式查找文本中的所有文本,excel,excel-formula,find,Excel,Excel Formula,Find,我试图在文本中查找文本,并将其显示在单元格中 我能找到这样的文本: =MIDA2,查找:,A2+1,查找/,A2,查找:,A2+1-查找:,A2-1 但是,我有多个输入,其中该单元格中有多个字符串,其值满足该条件。例如: 这只是一个例子:12345678/,另一个例子 :23455663/. 我的问题是,我如何才能捕捉到所有这些价值观,从:开始,到结束,而不仅仅是第一次 此外,如果可能,如何在字符串之间用一行分隔每个值。尝试以下用户定义的函数: Public Function FindAllT

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

我能找到这样的文本:

=MIDA2,查找:,A2+1,查找/,A2,查找:,A2+1-查找:,A2-1

但是,我有多个输入,其中该单元格中有多个字符串,其值满足该条件。例如:

这只是一个例子:12345678/,另一个例子 :23455663/.

我的问题是,我如何才能捕捉到所有这些价值观,从:开始,到结束,而不仅仅是第一次


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

尝试以下用户定义的函数:

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
只需确保使用带换行符的自定义项格式化单元格

编辑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

正如你所看到的,ABC没有出现在输出中。

如果你长期需要这样的文本搜索,那么就考虑学习如何使用支持正则表达式的VBA。@ TimiGeEeleSee我一直使用VBA,我只是通过一个快速的公式就足以得到想要的结果。然而,正如你所提到的,VBA可能是这个问题的答案。这工作完美无瑕,非常感谢Gary!明亮的加里,我还有一个问题,和你的代码有关。如果在文档中发现不符合相同条件的:的情况。例如,以:开头,但不以/结尾。它还会复制这些信息吗?我不希望发生这种情况,我想先核实一下。@CaptKenpa请看我的编辑1