簡體   English   中英

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

[英]Excel Vba bold some text on a string

我需要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變為粗體。

謝謝

這是答案的一個版本。 使用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