Color a part of a cell...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Good Day,

I have a cell with this text:

"1 - aaaaaaaa
2 - bbbbbbbb
3 - ccccccccc"

how to color in red, just the "1-, 2- and 3-"

anyone help?
Thanks
 
You can use this

Range("A1").Characters(Start:=1, Length:=2).Font.ColorIndex = 3
 
Good Day,

I have a cell with this text:

"1 - aaaaaaaa
2 - bbbbbbbb
3 - ccccccccc"

how to color in red, just the "1-, 2- and 3-"

anyone help?
Thanks

That can only be done if the text is an actual text string, and not the result
of a function.

The following will work assuming the leading number is a single digit as you
have shown:

-----------------------------
Option Explicit

Sub ColorNum()
Dim c As Range
Dim str As String
Dim i As Long
Dim char As String
Dim lRedLength As Long

For Each c In Selection
str = c.Text
c.Font.Color = vbBlack 'or whatever the base color is
For i = 1 To Len(str)
char = Mid(str, i, Len(str) + 1 - i)
If char Like "#*-?*" Then
lRedLength = InStr(i, str, "-") - i + 1
c.Characters(i, lRedLength).Font.Color = vbRed
i = i + lRedLength
End If
Next i
Next c

End Sub
 
Another way
select cell
in formula-line select part u want colored and click color


"Tiago" skrev:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top