Forum Discussion
How to extract multiple occurrences of a text enclosed between two same characters
- Oct 28, 2024
I hope this works for you (with the text in A5):
=LET(w, TEXTSPLIT(A5, "!"), n, COUNTA(w), s, SEQUENCE(, n), f, FILTER(w, ISEVEN(s)), TEXTJOIN(CHAR(10), TRUE, f))
Try below to extract the first occurrence:
=MID(A2, FIND(">>", A2) + 2, FIND("<<", A2) - FIND(">>", A2) - 2)
Follow to handle extract multiple occurrences:
Function ExtractTextBetweenTags(cell As Range) As String
Dim text As String
Dim result As String
Dim startPos As Long
Dim endPos As Long
text = cell.Value
result = ""
Do While InStr(text, ">>") > 0
startPos = InStr(text, ">>") + 2
endPos = InStr(text, "<<")
If endPos > startPos Then
result = result & Mid(text, startPos, endPos - startPos) & vbNewLine
text = Mid(text, endPos + 2)
Else
Exit Do
End If
Loop
ExtractTextBetweenTags = result
End Function