Changing Text Color

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
613
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I am trying to change font color if there is a ^ in front of the text. I have created a code below but can't seem to get it to work

VBA Code:
Private Sub Fill_Colour_Click()

Dim c As Range, x As Long
Dim ws As Worksheet

Set ws = Sheets("Job Card Master")
Set c = ws.Range("E13:E307")

For Each c In Selection
    If Right(c, 1) = Chr("^") Then c = Left(c, Len(c) - 1)
    If InStr(c, Chr("^")) > 0 Then
        c.Value = c.Value
        For x = 1 To Len(c)
            If Asc(Mid(c, x, 1)) = Chr("^") Then c.Characters(x, 1).vbRed
        Next
    End If
Next
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

afonsomira

Board Regular
Joined
Aug 11, 2020
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hello.
Try this one:
VBA Code:
Private Sub Fill_Colour_Click()

Dim c As Range, x As Long
Dim ws As Worksheet

Set ws = Sheets("Job Card Master")
Set c = ws.Range("E13:E307")

For Each c In Selection

'If ends whit a "^" then font color = to red
If Right(c, 1) = "^" Then
c.Font.Color = vbRed
'Else no ends whit a "^" then font color = to black
Else
c.Font.Color = vbBlack
End If
Next

End Sub
 

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
613
Office Version
  1. 2019
Platform
  1. Windows
Hello.
Try this one:
VBA Code:
Private Sub Fill_Colour_Click()

Dim c As Range, x As Long
Dim ws As Worksheet

Set ws = Sheets("Job Card Master")
Set c = ws.Range("E13:E307")

For Each c In Selection

'If ends whit a "^" then font color = to red
If Right(c, 1) = "^" Then
c.Font.Color = vbRed
'Else no ends whit a "^" then font color = to black
Else
c.Font.Color = vbBlack
End If
Next

End Sub
I`ve tried this but it jumps over the code if I try to run using F8. The text is to start with ^ rather than end with ^.
 

afonsomira

Board Regular
Joined
Aug 11, 2020
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Try this:
VBA Code:
Private Sub Fill_Colour_Click()

Dim c As Range, x As Long
Dim ws As Worksheet

Set ws = Sheets("Job Card Master")
Set c = ws.Range("E13:E307")

For Each c In Range("E13:E307")

'If begins whit a "^" then font color = to red
If Left(c, 1) = "^" Then
c.Font.Color = vbRed
'Else font color = to black
Else
c.Font.Color = vbBlack
End If
Next

End Sub

He runs for me here
 
Solution

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
613
Office Version
  1. 2019
Platform
  1. Windows
Thanks just ideal works brillant
 
Last edited by a moderator:

Forum statistics

Threads
1,144,699
Messages
5,725,824
Members
422,644
Latest member
BethGS

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
Top