I would like to color, for example, the 1s and 0s in a string such as "1001001001" as two different colors using a VBA Macro. Is this possible? Here is my attempted code
Sub changeTextColor()
Green = RGB(0, 255, 0)
Red = RGB(255, 0, 0)
Black = RGB(0, 0, 0)
'Select cell
Range("H2").Select
For x = 1 To 10
If ActiveCell.Value = "1" Then
'Change the text color if "1"
ActiveCell.Font.Color = Green
ElseIf ActiveCell.Value = "0" Then
'Change the text color if "0"
ActiveCell.Font.Color = Red
End If
ActiveCell.Offset(1, 0).Select
Next
End Sub
Something like this:
Sub changeTextColor()
Dim v, s, clr, rng, x
Set rng = Range("H2")
Do While rng.Value <> ""
v = rng.Value
For x = 1 To Len(v)
Select Case Mid(v, x, 1)
Case "1": clr = vbGreen
Case "0": clr = vbRed
Case Else: clr = vbBlack
End Select
rng.Characters(x, 1).Font.Color = clr
Next x
Set rng = rng.Offset(1, 0)
Loop
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.