How to HTML encode characters within a cell

hellothere4

New Member
Joined
May 9, 2011
Messages
17
Hello, I am trying to HTML encode characters within a cell. I am soo frustrated because search results bring up nothing. please help!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
OK I searched web and found this solution, but I have absolutely no idea how to incorporate it. What do I do after pasting it into VB? :confused:


Sub HTMLEncode()
Dim rng As Range
Dim i As Integer
Dim strValue As String

For Each rng In ActiveSheet.UsedRange.Cell<wbr>s
If Not rng.HasFormula Then
strValue = ""
For i = 1 To Len(rng.Value)
If isWebOK(Mid(rng.Value, i, 1)) Then
strValue = strValue & Mid(rng.Value, i, 1)
Else
strValue = strValue & "&#" & Format(Asc(Mid(rng.Value, i, 1)), "000") & ";"
End If
Next
rng.Value = strValue
End If
Next
End Sub

Function isWebOK(str As String)
isWebOK = (Asc(str) >= 32 And Asc(str) <= 123)
End Function

Basically all chars whose ascii code isn't between 32 and 123 will be converted to the HTML code. Don't forget to copy the sheet to a new one and run the code on the copy since you'll lose the original text.
 
Upvote 0
Hit ALT-F8 and run the HTMLEncode macro, although I will have a simpler code in a moment.
 
Upvote 0
I couldn't get that code to work and this is proving to be a little harder than I thought it would be. I or maybe someone else can get you some code.
 
Upvote 0
Code:
Sub test()
Dim c As Range, x As Long, d As Byte, e As String
For Each c In ActiveSheet.UsedRange
    If Not (c.HasFormula) Then
        e = c
        For x = 1 To Len(c)
            d = Asc(Mid(c, x, 1))
            If d > 31 And d < 124 Then e = Replace(e, Chr(d), "&#" & Format(d, "000") & ";")
        Next x
            c = e
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top