Changing text size within a shape using VBA

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Changing text size within a shape using VBA

Post by ABabeNChrist »

I use this piece of code the inserts a text box with a default font size of 11.

Code: Select all

    ActiveSheet.Shapes.AddTextbox( _
            msoTextOrientationHorizontal, _
            ActiveCell.Left, _
            ActiveCell.Top, _
            Width:=100, _
            Height:=20).Select
I wanted to increase the font size to 14. I first tried to record a macro to get an idea of the correct way of doing this, unfortunately that does not seem to work with shapes. So I played around trying different ways and this is what I was able to get, so my question is, is this the correct approach.

Code: Select all

    With Selection
        .Font.Size = 14
    End With

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Changing text size within a shape using VBA

Post by HansV »

You don't have to select the shape. Try this:

Code: Select all

    With ActiveSheet.Shapes.AddTextbox( _
            msoTextOrientationHorizontal, _
            ActiveCell.Left, _
            ActiveCell.Top, _
            Width:=100, _
            Height:=20)
        .TextFrame2.TextRange.Font.Size = 14
    End With
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Changing text size within a shape using VBA

Post by ABabeNChrist »

Works great
I know sometimes I ask very simple questions, I do so for clarification and that it may be also helpful for others who may be searching for this same problem and find their way here at Eileen’s Lounge.
Thank you Hans :cheers: