VBA:如何格式化文本框文本的不同部分?

时间:2018-07-27 14:19:36

标签: vba excel-vba

我希望更新大量电子表格中特定文本框中的文本。文本框中的某些文本需要与其他文本不同的格式。在VBA中,如何给文本框文本的不同部分提供不同的格式?

1 个答案:

答案 0 :(得分:2)

执行此操作的方法是访问Characters集合。这使您可以将特定格式应用于不同的字符范围:

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

相关问题