I am writing a VBA to bold and red the text in a cell with the following condition
- any words insided "<" and ">" or "{" and "}" (e.g. "xyz <abc> xyz <123>" becomes "xyz <abc> xyz <123>" or "xyz {abc} xyz <123>" becomes "xyz {abc} xyz <123>"
the following code is what i try, it work normally, but when I test for the code with some strange string, for example, my selected cell with text starting by some strings which already in bold format, e.g. "a ""<abc>"" a" or "xyz <abc>", they will return "a <abc> a" (the bold format of first "a" disappear)
"xyz yz <abc>" (bold format of "xyz" disappear, "yz" added in between and "<abc>" did not turn red, the strangest thing is that "xyz <abc>" is still showing on formula bar which is different from what the cells value (xyz yz <abc>) display
Is there any bugs I did not notified? thanks.
- any words insided "<" and ">" or "{" and "}" (e.g. "xyz <abc> xyz <123>" becomes "xyz <abc> xyz <123>" or "xyz {abc} xyz <123>" becomes "xyz {abc} xyz <123>"
the following code is what i try, it work normally, but when I test for the code with some strange string, for example, my selected cell with text starting by some strings which already in bold format, e.g. "a ""<abc>"" a" or "xyz <abc>", they will return "a <abc> a" (the bold format of first "a" disappear)
"xyz yz <abc>" (bold format of "xyz" disappear, "yz" added in between and "<abc>" did not turn red, the strangest thing is that "xyz <abc>" is still showing on formula bar which is different from what the cells value (xyz yz <abc>) display
Is there any bugs I did not notified? thanks.
Code:
Sub redbold()
Dim cell As Range
Dim pos, startpos As Long
Dim targetref As Boolean
For Each cell In Selection
For pos = 1 To Len(cell.Value)
If (Mid(cell.Value, pos, 1) = "<") Or (Mid(cell.Value, pos, 1) = "{") Then
startpos = pos
targetref = True
ElseIf targetref And (Mid(cell.Value, pos, 1) = ">" Or Mid(cell.Value, pos, 1) = "}") Then
With cell.Characters(startpos, pos - startpos + 1).Font
.ColorIndex = 3
.Bold = True
End With
targetref = False
End If
Next pos
Next cell
End Sub
Last edited: