Excel Vba在字符串上加粗一些文本

时间:2017-12-08 03:18:08

标签: excel string vba excel-vba format

我需要一些关于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_nameGuest_age变为粗体。

由于

2 个答案:

答案 0 :(得分:2)

以下是答案的一个版本。使用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

答案 1 :(得分:0)

使用InStr在范围的.value2属性中定位每个属性,并将.font.bold = true应用于子字符串的.Characters属性。