Color font before and after delimiter

RIchavalues

New Member
Joined
Feb 24, 2016
Messages
25
Office Version
  1. 2019
Platform
  1. Windows
Hi Experts,

Am new to VBA and would like to know if we can color text in a cell based on delimiter (-).

Say I have text "Welcome to - MrExcel" in cell "D22" then I want font color to be like this "Welcome to - MrExcel".

i.e., "Welcome to -" including hypen (-) in one color and "MrExcel" i.e., after hypen(-) in one color.

I tried to search in forum and also google but no help. Really appreciate any help provided.

Thanks,
Richa
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi Richa

Welcome to the board.

Try the following with the hyphenated text in cell D22

Code:
Public Sub Test()

Dim oCell As Range
Dim iHyphenPos As Integer

Set oCell = ActiveSheet.Range("D22")

iHyphenPos = (InStr(1, oCell.Text, "-")) 'Character position of hyphen

'May need tests in addition to "hyphen exists" at next line

If iHyphenPos <> 0 Then ' If not zero, hyphen was found
    
    oCell.Characters(1, iHyphenPos - 1).Font.Color = RGB(0, 0, 255) 'Blue
    oCell.Characters(iHyphenPos + 1, (Len(oCell.Text)) - iHyphenPos).Font.Color = RGB(255, 0, 0) 'Red

End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

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