Sws750
Sws750

Reputation: 33

VBA: How to format different parts of textbox text differently?

I'm looking to update the text in a specific textbox in a large number of spreadsheets. Some of the text in the textbox needs to be formatted differently from some of the other text. In VBA, how can I give separate parts of the textbox's text different formatting?

Upvotes: 0

Views: 1412

Answers (1)

David Zemens
David Zemens

Reputation: 53663

The way to do this is by accessing the Characters collection. This allows you to apply specific formats to different character ranges:

Sub tbformats()

Dim tb As Shape

Set tb = ThisWorkbook.Worksheets(1).Shapes(1)

'Apply bold to the first 10 characters:
tb.TextFrame2.TextRange.Characters(1, 10).Font.Bold = True

'Apply italic to characters 3-5
tb.TextFrame2.TextRange.Characters(3, 5).Font.Italic = True

' make the last 5 characters red:
tb.TextFrame2.TextRange.Characters(44, 5).Font.Fill.ForeColor.RGB = RGB(255, 0, 0)
End Sub

enter image description here

Upvotes: 2

Related Questions