Partial change font colour within cell - VBA

jimsjams

New Member
Joined
Nov 6, 2008
Messages
29
A friend wanted help to write a macro to change the text colour (to turn blue text to black and red text to blue) I thought I could help and so made this:

Sub changecol()

For Each Cell In Selection
If Cell.Font.ColorIndex = 37 Then
Cell.Font.ColorIndex = 1
End If
Next

For Each Cell In Selection
If Cell.Font.ColorIndex = 3 Then
Cell.Font.ColorIndex = 37
End If
Next

End Sub

I then found out that his cells had multiple font colours within in them (blue, black and red) and only part of the cell text should be changed.

This is much more complicated. I was wondering whether there is a way to use the cell.characters.font property to count the starting character number and length of say the blue text in order to convert it to black.

Would this even be possible?

Any thoughts greatly appreciated!

Thanks,

James
 
What if I need to have everything that is in square brackets highlighted as well as all HTML tags? I have tried modifying the above but was only able to get the brackets turned to red whereas the rest of the text was left in black:

Code:
Sub Format_Characters_In_Found_Cell()
Dim Found As Range, x As String, FoundFirst As Range


x = "["
x = "]"
On Error Resume Next
Set Found = Cells.Find(what:=x, LookIn:=xlValues, LookAt:=xlPart)


If Not Found Is Nothing Then
    Set FoundFirst = Found
    Do
      'Format "x"
        With Found.Characters(Start:=InStr(Found.Text, x), Length:=Len(y))
            .Font.ColorIndex = 3
            .Font.Bold = False
        End With
        Set Found = Cells.FindNext(Found)
    Loop Until FoundFirst.Address = Found.Address
Else
    MsgBox x & " could not be found.", , " "
End If


End Sub

Any ideas?

Thanks!
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I guess I need to add RegEx here, something like
^\[.*?\] for the square brackets for instance. Though, I am not sure how to embed this expression.

Please help.
 
Upvote 0

Forum statistics

Threads
1,216,127
Messages
6,129,022
Members
449,481
Latest member
joaotcosta23

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