Sub ReplaceRichText()
Dim xCell As Range
Dim rng As Range
Dim BoldArray(2, 100) As Integer
Dim ItalicArray(2, 100) As Integer
Dim UnderlineArray(2, 100) As Integer
Dim CenterArray(2, 100) As Integer
Dim i As Integer
Dim Find As Integer
Dim Start As Integer
Dim xPos As Integer
Dim yLen As Integer
Dim ParsedString As String
Set rng = Selection
For Each xCell In rng
xCell.Value = Replace(xCell.Value, "{div}", "") 'Remove div RTF code
xCell.Value = Replace(xCell.Value, "{/div}", "") 'Remove div RTF code
xCell.Value = Replace(xCell.Value, " ", " ") 'Remove "non-blank space" RTF code
xCell.Value = Replace(xCell.Value, vbCr & vbCr, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbCr & vbCrLf, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbCr & vbLf, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbCrLf & vbCrLf, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbCrLf & vbCr, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbCrLf & vbLf, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbLf & vbLf, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbLf & vbCr, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, vbLf & vbCrLf, vbLf) 'Remove duplicated lines
xCell.Value = Replace(xCell.Value, "{font face=Arial size=2 color=black}", "") 'Remove font RTF code
xCell.Value = Replace(xCell.Value, "{/font}", "") 'Remove font RTF code
xCell.Value = Replace(xCell.Value, "{font color=black}", "") 'Remove font RTF code
xCell.Value = Replace(xCell.Value, "{font face=""Cambria Math"" size=2 color=black}", "") 'Remove font RTF code
xCell.Value = Replace(xCell.Value, ">", "}") 'Replace Greater Than RTF code with {
xCell.Value = Replace(xCell.Value, "<", "{") 'Replace Greater Than RTF code with {
xCell.Value = Replace(xCell.Value, """, """") 'Replace Quote RTF code with "
'Bold
ParsedString = Replace(xCell.Value, "{em}", "") 'Remove Italic RTF code
ParsedString = Replace(ParsedString, "{/em}", "") 'Remove Italic RTF code
ParsedString = Replace(ParsedString, "{u}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{/u}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{center}", "") 'Remove Center Align RTF code
ParsedString = Replace(ParsedString, "{/center}", "") 'Remove Center Align RTF code
xPos = InStr(ParsedString, "{strong}")
yLen = InStr(ParsedString, "{/strong}") - xPos - 8
i = 1
Start = 1
Do While xPos } 0
BoldArray(1, i) = xPos
BoldArray(2, i) = yLen
Start = InStr(Start, ParsedString, "{strong}") + 2
Debug.Print "Bold", xCell.Address, xPos, yLen, Start
xPos = InStr(Start, ParsedString, "{strong}") - (17 * i)
yLen = InStr(Start + yLen + 17, ParsedString, "{/strong}") - InStr(Start, ParsedString, "{strong}") - 8
i = i + 1
Loop
'Italic
ParsedString = Replace(xCell.Value, "{strong}", "") 'Remove Bold RTF code
ParsedString = Replace(ParsedString, "{/strong}", "") 'Remove Bold RTF code
ParsedString = Replace(ParsedString, "{u}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{/u}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{center}", "") 'Remove Center Align RTF code
ParsedString = Replace(ParsedString, "{/center}", "") 'Remove Center Align RTF code
xPos = InStr(ParsedString, "{em}")
yLen = InStr(ParsedString, "{/em}") - xPos - 4
i = 1
Start = 1
Do While xPos } 0
ItalicArray(1, i) = xPos
ItalicArray(2, i) = yLen
Start = InStr(Start, ParsedString, "{em}") + 2
Debug.Print "Italic", xCell.Address, xPos, yLen, Start
xPos = InStr(Start, ParsedString, "{em}") - (9 * i)
yLen = InStr(Start + yLen + 9, ParsedString, "{/em}") - InStr(Start, ParsedString, "{em}") - 4
i = i + 1
Loop
'Underline
ParsedString = Replace(xCell.Value, "{strong}", "") 'Remove Bold RTF code
ParsedString = Replace(ParsedString, "{/strong}", "") 'Remove Bold RTF code
ParsedString = Replace(ParsedString, "{em}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{/em}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{center}", "") 'Remove Center Align RTF code
ParsedString = Replace(ParsedString, "{/center}", "") 'Remove Center Align RTF code
xPos = InStr(ParsedString, "{u}")
yLen = InStr(ParsedString, "{/u}") - xPos - 3
i = 1
Start = 1
Do While xPos } 0
UnderlineArray(1, i) = xPos
UnderlineArray(2, i) = yLen
Start = InStr(Start, ParsedString, "{u}") + 2
Debug.Print "Underline", xCell.Address, xPos, yLen, Start
xPos = InStr(Start, ParsedString, "{u}") - (7 * i)
yLen = InStr(Start + yLen + 7, ParsedString, "{/u}") - InStr(Start, ParsedString, "{u}") - 3
i = i + 1
Loop
'Center
ParsedString = Replace(xCell.Value, "{strong}", "") 'Remove Bold RTF code
ParsedString = Replace(ParsedString, "{/strong}", "") 'Remove Bold RTF code
ParsedString = Replace(ParsedString, "{em}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{/em}", "") 'Remove Underline RTF code
ParsedString = Replace(ParsedString, "{u}", "") 'Remove Center Align RTF code
ParsedString = Replace(ParsedString, "{/u}", "") 'Remove Center Align RTF code
xPos = InStr(ParsedString, "{center}")
yLen = InStr(ParsedString, "{/center}") - xPos - 8
i = 1
Start = 1
Do While xPos } 0
CenterArray(1, i) = xPos
CenterArray(2, i) = yLen
Start = InStr(Start, ParsedString, "{center}") + 2
Debug.Print "Center", xCell.Address, xPos, yLen, Start
xPos = InStr(Start, ParsedString, "{center}") - (17 * i)
yLen = InStr(Start + yLen + 17, ParsedString, "{/center}") - InStr(Start, ParsedString, "{center}") - 8
i = i + 1
Loop
xCell.Value = Replace(xCell.Value, "{strong}", "")
xCell.Value = Replace(xCell.Value, "{/strong}", "")
xCell.Value = Replace(xCell.Value, "{em}", "")
xCell.Value = Replace(xCell.Value, "{/em}", "")
xCell.Value = Replace(xCell.Value, "{u}", "")
xCell.Value = Replace(xCell.Value, "{/u}", "")
xCell.Value = Replace(xCell.Value, "{center}", "")
xCell.Value = Replace(xCell.Value, "{/center}", "")
i = 1
Do While BoldArray(1, i) } 0
xCell.Characters(BoldArray(1, i), BoldArray(2, i)).Font.Bold = True
Debug.Print "FrmtB", xCell.Address, BoldArray(1, i), BoldArray(2, i)
i = i + 1
Loop
i = 1
Do While ItalicArray(1, i) } 0
xCell.Characters(ItalicArray(1, i), ItalicArray(2, i)).Font.Italic = True
i = i + 1
Loop
i = 1
Do While UnderlineArray(1, i) } 0
xCell.Characters(UnderlineArray(1, i), UnderlineArray(2, i)).Font.Underline = True
Debug.Print "FrmtU", xCell.Address, UnderlineArray(1, i), UnderlineArray(2, i)
i = i + 1
Loop
i = 1
Do While CenterArray(1, i) } 0
xCell.Characters(CenterArray(1, i), CenterArray(2, i)).HorizontalAlignment = xlCenter
Debug.Print "FrmtC", xCell.Address, CenterArray(1, i), CenterArray(2, i)
i = i + 1
Loop
Erase BoldArray
Erase ItalicArray
Erase UnderlineArray
Erase CenterArray
Next
End Sub
Bookmarks