Change the font color for a piece of text in a cell

I have cells that will contain a lower value

"Image not allowed|png" 

I want to change color | png yourself or something else after "|"

Now I'm trying to change the font color using the code below

 Cells(4,2).Font.Color = RGB(255, 50, 25) 

It will change the font color of the whole cell, is it possible to change only the selected text color ( |png ) using VBA?

+8
source share
3 answers

This should be a good start:

 Sub vignesh() Dim StartChar As Integer, _ LenColor As Integer For i = 1 To 5 With Sheets("Sheet1").Cells(i, 1) StartChar = InStr(1, .Value, "|") If StartChar <> 0 Then LenColor = Len(.Value) - StartChar + 1 .Characters(Start:=StartChar, Length:=LenColor).Font.Color = RGB(255, 0, 0) End If End With Next i End Sub 
+11
source

Yes it is possible. A good way to learn the Excel object model is to use a macro recorder to record a macro in which you manually perform the manipulation you are interested in.

In this case you can use:

 Cell.Characters(Start:=1, Length:=5).Font 

set the font properties of a substring in a cell.

+7
source

Is it possible to change only the selected text color

Plain

 Option Explicit Sub Test() With Selection.Font .ColorIndex = 3 End With End Sub 
0
source

Source: https://habr.com/ru/post/1263922/


All Articles