2

I want to remove all HTML tags from a string in Excel VBA.

For example:

before_text = "text1 <br> text2 <a href = 'www.data.com' id = 'data'>text3</a> text4"

after_text = RemoveTags(before_text)

Result:

after_text = "text1  text2 text3 text4"
1
  • 2
    Possible duplicate of Stripping HTML From A String Commented Nov 18, 2017 at 14:48

2 Answers 2

5
vbscript.regexp

Code:

Function RemoveHTML(text As String) As String
    Dim regexObject As Object
    Set regexObject = CreateObject("vbscript.regexp")

    With regexObject
        .Pattern = "<!*[^<>]*>"    'html tags and comments
        .Global = True
        .IgnoreCase = True
        .MultiLine = True
    End With

    RemoveHTML = regexObject.Replace(text, "")
End Function
Sign up to request clarification or add additional context in comments.

Comments

1

Building on @zhihar's reply, to make this strip all HTML from the selected cell you can iterate through the selection.

Function RemoveHTML(text As String) As String
    Dim regexObject As Object
    Set regexObject = CreateObject("vbscript.regexp")

    With regexObject
        .Pattern = "<!*[^<>]*>"    'html tags and comments
        .Global = True
        .IgnoreCase = True
        .MultiLine = True
    End With

    RemoveHTML = regexObject.Replace(text, "")
End Function


Sub StripHtmlSelected()
    For Each Cell In Selection
        If Not Cell.HasFormula Then
            Cell.Value = RemoveHTML(Cell.Value)
        End If
    Next Cell
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.