color part of a cell

Tody03

New Member
Joined
Jun 21, 2014
Messages
48
In my worksheet I have in each cell different data (text & numbers)
How can I write a macro that will color in red the numbers between the first comma and the second.

122,-25%,Brkfst,NRF

<tbody>
</tbody>
In the above sample I want to paint -25% in red

In some cells the space between the commas is empty
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Assuming your cells contain text and not formulas, the following macro will do what you want for all cells in the current selection...
Code:
[table="width: 500"]
[tr]
	[td]Sub ColorBetween2ndAnd3rdComma()
  Dim Cell As Range
  For Each Cell In Selection
    If Len(Cell.Value) Then Cell.Characters(InStr(Cell.Value, ",") + 1, Len(Split(Cell.Value, ",")(1))).Font.Color = vbRed
  Next
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
If the cells are in column A. Similar to Rick's.
Code:
Sub t()
Dim sh As Worksheet, c As Range, spl As Variant
Set sh = ActiveSheet
    With sh
        For Each c In .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
            spl = Split(c.Value, ",")
            c.Characters(InStr(c.Value, spl(1)), Len(spl(1))).Font.Color = vbRed
        Next
    End With
End Sub
 
Upvote 0
Try this, it works with A1:A8 so adjust the range to suit.
Code:
Dim cl As Range
Dim ch As Object
Dim I As Long
Dim pos As Long
Dim strSecond As String

    For Each cl In Range("A1:A8")

        pos = InStr(cl.Value, ",")

        If pos > 0 Then
            strSecond = Split(cl.Value, ",")(1)

            For I = pos + 1 To pos + Len(strSecond)

                Set ch = cl.Characters(I, 1)
                ch.Font.Color = vbRed

            Next I

        End If
    Next cl
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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