Totos
Totos

Reputation: 31

Is there a way to change font color of portion of a string in VBA userform's label?

I'm trying to change the color of a portion of a string that is loaded into userform's label based on certain conditions. My attempt is to find the starting character and length of the string I want to format by using instr function and then loop through individual characters and change their .forecolor property.

startpoint = InStr(showformula, ListBoxValue)
endpoint = startpoint + Len(ListBoxValue)

For i = startpoint To endpoint
  LabelMain.Characters(i).ForeColor = RGB(255, 0, 0)
Next i

Unfortunately, I am getting error on the LabelMain.Characters(i).ForeColor = RGB(255, 0, 0) as it looks like I cannot select individual characters in VBA's label userform.

Is there another way to change the font color of only portion of the label's caption?

Thank you!

Upvotes: 0

Views: 1415

Answers (1)

Dave Scott
Dave Scott

Reputation: 153

Nope. If it's just a one off can just split your text into multiple labels and sit them on top of each other.

If your wanting something programmtic it would be a lot trickier, only solution I can think of would be to :

  • Create array of the widths of all characeters in your font set.

  • Calculate distance from edge of label where highlight text starts

  • Replace text with spaces equal to highlight text lenght

  • Create a new label over the top in correct position based on new distance.

     set Lbl = MyForm.Contorls.Add("Forms.Label.1", "MyNewLabelName",true)
     'Set properties of the lbl
    

Upvotes: 1

Related Questions