【问题标题】:Excel Vba bold some text on a stringExcel Vba在字符串上加粗一些文本
【发布时间】:2019-03-01 05:28:41
【问题描述】:
我需要一些有关 excel 的帮助。
我有一个单元格通过代码宏获取值:
Range("A1").Value = "This Year we are having our guest " &Guest_name &" who is currently " &Guest_age &"years old, spending holidays with us"
所以我只是想知道如何让 Guest_name 和 Guest_age 加粗。
谢谢
【问题讨论】:
标签:
string
vba
excel
format
【解决方案1】:
这是答案的一个版本。使用Characters 指示vba 从哪里开始和结束。
Sub test()
Dim guest_name, guest_name_len As Integer
Dim guest_age, guest_age_len As Integer
guest_name = "tester"
guest_name_len = Len(guest_name)
guest_age = "999"
guest_age_len = Len(guest_age)
Debug.Print guest_name_len & " / " & guest_age_len
Range("A1").Value = "This Year we are having our guest " & guest_name & " who is currently " & guest_age & "years old, spending holidays with us"
With Range("A1").Characters(Start:=35, Length:=guest_name_len).Font '35 characters counted (this year....)
.FontStyle = "bold"
End With
With Range("A1").Characters(Start:=35 + guest_name_len + 18, Length:=guest_age_len).Font '18 characters counted (who is... years old)
.FontStyle = "bold"
End With
End Sub
【解决方案2】:
使用 InStr 在范围的 .value2 属性中定位每个,并将 .font.bold = true 应用于子字符串的 .Characters 属性。