How to change font colors of selected characters of a text string variable?

ChetShannon

Board Regular
Joined
Jul 27, 2007
Messages
133
Office Version
  1. 365
Platform
  1. Windows
I have seen many ways to change part of specific cells text color but I am looking to change the colors of select characters in a text string *variable*. I would think this would be faster than referencing a specific cell reference address and then changing the select characters in the cell text string.

So to be more clear I want to change the colors of characters in a text string variable. Can I do this?

SampleString ="this is the test string"
For example maybe I just want to change the font color of the word "test". There is no assigned cell position yet. Is this possible to do?

Thanks!
Chet
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
It would be nice, but I don't believe it's possible. In VBA, a string value is just a collection of characters, no formatting information. You can change the color of part of a string in a cell because the cell has formatting information in addition to just the cell value.
 
Upvote 0
No, not the way you want.

If you'd like to include attributes in a string text variable you could use markup. You'd need to decode it when applying the string.
 
Upvote 0
This is a simple code that will work. It will only do the first instant but you could build off it to find each time that word is in a string.


Sub TextColor()

Dim MyText As String

MyText = "test"

Range("A1").Characters(InStr(1, Range("A1").Value2, MyText), Len(MyText)).Font.ColorIndex = 5

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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