VBA to Center Text in a Shape

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I would appreciate any help...

I have a macro that creates a shape and then lets me format it. For example, this part of the code allows me to bold the text:

With MyShape.TextFrame
.Characters.Font.Bold = False
End With

What I would also like to do is to middle center the text in the shape. I did some research and tried adding the following two lines into the With/End With loop, but get an error:

.HorizontalAnchor = msoAnchorCenter
.VerticalAnchor = msoAnchorTop

Can anyone help me on where I am going wrong?

Thanks,

Mike
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You need to use TextFrame2 for that
VBA Code:
    With myshape.TextFrame2
      .VerticalAnchor = msoAnchorMiddle
      .HorizontalAnchor = msoAnchorCenter
    End With
 
Upvote 0
You need to use TextFrame2 for that
VBA Code:
    With myshape.TextFrame2
      .VerticalAnchor = msoAnchorMiddle
      .HorizontalAnchor = msoAnchorCenter
    End With
Thanks! Appreciate it.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,887
Messages
6,175,199
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top