Home » excel » excel – Bold only one portion in VBA

excel – Bold only one portion in VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am currently building a macro that will create a Powerpoint from an excel workbook. I have two cells that I currently combine into one powerpoint textbox. I want to be able to bold one cell value in the textbox. Is this possible?

This is my current code:

Proj = Sheets("Bay du Nord").Range("A23")
Proj2 = Sheets("Bay du Nord").Range("B23")


Set LCProj = Slide2.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=210, Top:=265, Width:=110, Height:=100)
With LCProj
    .Name = "LC Proj"
    With .TextFrame.TextRange
        .Text = Proj & vbNewLine & Proj2 & vbNewLine & "kg CO2/BOE"
        .Font.Size = 12
        .Font.Bold = False
        .ParagraphFormat.Alignment = ppAlignCenter
    End With
    With .Fill
        .TwoColorGradient msoGradientHorizontal, 2
        .ForeColor.RGB = RGB(140, 0, 0)
        .BackColor.RGB = RGB(180, 5, 0)
    End With
    .Shadow.Type = msoShadow14
End With

Say I want to bold the value that is in cell A23 (Proj) and leave the value in B23 (Proj2) the same.

Thanks

How to&Answers:

You can use TextRange.Characters() to change the properties of a subset of the original TextRange like so:

Proj = Sheets("Bay du Nord").Range("A23")
Proj2 = Sheets("Bay du Nord").Range("B23")


Set LCProj = Slide2.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=210, Top:=265, Width:=110, Height:=100)
With LCProj
    .Name = "LC Proj"
    With .TextFrame.TextRange
        .Text = Proj & vbNewLine & Proj2 & vbNewLine & "kg CO2/BOE"
        .Font.Size = 12
        .Font.Bold = False
        .ParagraphFormat.Alignment = ppAlignCenter
            With .Characters(1, len(Proj))
                .Font.Bold = True
            End With
    End With
    With .Fill
        .TwoColorGradient msoGradientHorizontal, 2
        .ForeColor.RGB = RGB(140, 0, 0)
        .BackColor.RGB = RGB(180, 5, 0)
    End With
    .Shadow.Type = msoShadow14
End With