Excel Vba bold some text on a string

15,590

Here is one version of the answer. Using Characters to instruct vba where to start and end.

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
Share:
15,590

Related videos on Youtube

Juan Hernandez
Author by

Juan Hernandez

Updated on June 04, 2022

Comments

  • Juan Hernandez
    Juan Hernandez about 2 years

    i need some help with 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.

    Thanks