0

I trying to remove the html tags using the code below but it does not do anything. I have tried running it again but to no use. Could someone please advise what could be the issue. Attaching a snapshot of the file with html tags. For info, the data comes from the MS Access and the MS Access links to Sharepoint lists.

enter image description here

Sub Import_AccessData()
 Dim strtKeyMsgRange As Range
 Dim KeyMsgRange As Range
 Dim KeyMsgRangeCell As Range
 Dim endKeyMsgRangeCell As Range

Set strtKeyMsgRange = Range("B2")
Set endKeyMsgRange = Range("AC13")

Set KeyMsgRange = Range(strtKeyMsgRange, endKeyMsgRange)

For Each KeyMsgRangeCell In KeyMsgRange
   a = StripHTML(KeyMsgRangeCell)
   KeyMsgRangeCell.Value = a
Next KeyMsgRangeCell

End Sub


Public Function StripHTML(cell As Range) As String
     Dim RegEx As Object
     Set RegEx = CreateObject("vbscript.regexp")

     Dim sInput As String
     Dim sOut As String


     sInput = cell.Value

     With RegEx
   .Global = True
   .IgnoreCase = True
   .MultiLine = True
   .Pattern = "<[^>]+>" 'Regular Expression for HTML Tags.
   .Pattern = "&nbsp;"
   .Pattern = "&amp;"
 End With

 sOut = RegEx.Replace(sInput, "")
 StripHTML = sOut
 Set RegEx = Nothing
End Function
4
  • regexes manipulating html only reads to madness. Commented Oct 10, 2012 at 19:26
  • I think Jeff Atwood says it best. Commented Oct 10, 2012 at 19:27
  • I am not hell-bent to use regex. I just have a problem at hand(viz html tags that come from importing data from Sharepoint) and I will employ any method to get rid of those tags. Any suggestions please? Commented Oct 10, 2012 at 19:35
  • @MarcB: To be fair, asker isn't trying to parse HTML, only strip out tags. Commented Oct 10, 2012 at 19:45

1 Answer 1

1

You are setting the Pattern property multiple times, it will only retain the last value assigned (&amp;).

You need to use 3 regular expressions("<[^>]+>" => "", "&nbsp;" => " ", "&amp;" => "&"), or one expression that matches all of your inputs ("(&amp;)|(&nbsp;)|(<[^>]+>)" => "") to actually do this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone for looking into this. ("(&amp;)|(&nbsp;)|(<[^>]+>)") worked. Thanks Joaquim R.

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.