VBA to change text colour

DeekMan

New Member
Joined
Apr 20, 2019
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I have been trying to get code to change the colour of some text in a cell, but to leave part black.

I attached a snip from my spreadsheet, and the code that I am running. What I am trying to achieve is "NAME" in black, "(00:00)" in Red, as per the first row, which I have done manually. The (00:00) will always have 1 space after the last letter. And to be fair, I have copied this code.

Any help would be appreciated.

VBA Code:
Sub ColourSomeText()

Dim P As Integer
Dim l As Integer
Dim wb As Workbook
Dim ws1 As Worksheet
Dim LastRow As Long
Dim St1 As String

Set wb = ThisWorkbook
Set ws1 = wb.Sheets("EMR")

LastRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To LastRow

P = InStr(Cells(i, 5).Value, " ")
l = Len(Cells(i, 5).Value)
    
    ws1.Cells(i, 5).Characters(Start:=P + 1, Length:=8).Font.ColorIndex = 3
Next

End Sub
 

Attachments

  • MrExcel Text Colour.JPG
    MrExcel Text Colour.JPG
    106.4 KB · Views: 9

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You can probably search for the "("

VBA Code:
Sub colotText()
    Dim ws As Worksheet, rng As Range, c As Range, x
    Set ws = ActiveSheet
    With ws
        Set rng = .Range("E2:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)

        For Each c In rng.Cells

            x = InStr(c, "(")
            c.Characters(Start:=1, Length:=x - 1).Font.Color = vbRed
            c.Characters(Start:=x, Length:=7).Font.Color = vbBlack
        Next
    End With

End Sub
 
Upvote 0
It's a shame you only gave an example. So the constant is a space " " or the parenthesis "("?
However use the following, adjust the " " or "(" to whatever you need:

VBA Code:
Sub ColourSomeText()
  Dim p As Integer, n As Long, c As Range
  For Each c In Sheets("EMR").Range("E2", Sheets("EMR").Range("E" & Rows.Count).End(xlUp))
    p = InStr(c.Value, " ")   '<--- Change to "(" if you prefer
    If p > 0 Then c.Characters(p, Len(c.Value)).Font.ColorIndex = 3
  Next
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Solution
It's a shame you only gave an example. So the constant is a space " " or the parenthesis "("?
However use the following, adjust the " " or "(" to whatever you need:

VBA Code:
Sub ColourSomeText()
  Dim p As Integer, n As Long, c As Range
  For Each c In Sheets("EMR").Range("E2", Sheets("EMR").Range("E" & Rows.Count).End(xlUp))
    p = InStr(c.Value, " ")   '<--- Change to "(" if you prefer
    If p > 0 Then c.Characters(p, Len(c.Value)).Font.ColorIndex = 3
  Next
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Hi Dante,

The code worked perfectly, but only with the "(" change.

Thank you.
 
Upvote 1
You can probably search for the "("

VBA Code:
Sub colotText()
    Dim ws As Worksheet, rng As Range, c As Range, x
    Set ws = ActiveSheet
    With ws
        Set rng = .Range("E2:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)

        For Each c In rng.Cells

            x = InStr(c, "(")
            c.Characters(Start:=1, Length:=x - 1).Font.Color = vbRed
            c.Characters(Start:=x, Length:=7).Font.Color = vbBlack
        Next
    End With

End Sub
Hi Dave,

Unfortunately, this worked in reverse...

But many thanks for your answer.

DW
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,304
Members
448,886
Latest member
GBCTeacher

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