简体   繁体   English

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

[英]Excel Vba bold some text on a string

i need some help with excel. 我需要Excel的帮助。

I have a cell that get value by macro with code: 我有一个通过代码通过宏获取价值的单元格:

Range("A1").Value = "This Year we are having our guest " &Guest_name &" who is currently " &Guest_age &"years old, spending holidays with us"

So i'm just wondering how can I get the Guest_name and Guest_age to be bold. 所以我只是想知道如何使Guest_nameGuest_age变为粗体。

Thanks 谢谢

Here is one version of the answer. 这是答案的一个版本。 Using Characters to instruct vba where to start and end. 使用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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM