Last active
March 22, 2017 19:35
-
-
Save wizard04wsu/cf842fbe1e12c8a7fc1d571a38abe99d to your computer and use it in GitHub Desktop.
Strip HTML tags from selected cells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'requires reference to Microsoft VBScript Regular Expressions 5.5 | |
Sub stripHTML() | |
Dim cell As Object | |
For Each cell In Selection | |
If Not (IsEmpty(cell) Or IsNumeric(cell) Or cell.HasFormula) Then | |
On Error Resume Next | |
cell.Value = doStripHTML(cell.Formula) | |
If Err > 0 Then | |
cell.Value = doStripHTML(cell.Value) | |
End If | |
On Error GoTo 0 | |
End If | |
Next | |
End Sub | |
Private Function doStripHTML(strHTML) As String | |
'Strips the HTML tags from strHTML | |
Dim rxp | |
Set rxp = New RegExp | |
rxp.IgnoreCase = True | |
rxp.Global = True | |
rxp.Pattern = "</?[a-z]+([^<]|\n)*?>|<!--(.|\n)*?-->" | |
'Replace all HTML tag matches with the empty string | |
strHTML = rxp.Replace(strHTML, "") | |
strHTML = Replace(strHTML, "<", "<") | |
strHTML = Replace(strHTML, ">", ">") | |
strHTML = Replace(strHTML, """, """") | |
strHTML = Replace(strHTML, "&", "&") | |
rxp.Pattern = "(?:[\s\xA0\n]| )+" | |
strHTML = rxp.Replace(strHTML, " ") | |
strHTML = Trim(strHTML) | |
If Left(strHTML, 1) = "=" Then strHTML = "'" & strHTML | |
doStripHTML = strHTML | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment