Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Excel Vba加粗字符串上的某些文本_String_Vba_Excel_Format - Fatal编程技术网

String Excel Vba加粗字符串上的某些文本

String Excel Vba加粗字符串上的某些文本,string,vba,excel,format,String,Vba,Excel,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" 因此,我只是想知道如何将客人姓名和客人年龄设为粗体 感谢使用InStr在范围的.value2属性中查找每个属性,并将.

我需要一些excel的帮助

我有一个单元格,通过宏代码获取值:

Range("A1").Value = "This Year we are having our guest " &Guest_name &" who is currently " &Guest_age &"years old, spending holidays with us"
因此,我只是想知道如何将
客人姓名
客人年龄
设为粗体


感谢使用InStr在范围的.value2属性中查找每个属性,并将.font.bold=true应用于子字符串的.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