Changing the Shape of a textbox in a chart in VBA.

ziggeh

New Member
Joined
Mar 19, 2009
Messages
6
Hi,

I am using Office 2007.

I was wondering if you were able to change the shape of the text box in VBA. I am trying to write some code which populates ovals on a chart with text inside. I can do this manually but when I record the macro it does not record the commands.

Also is there a good guide somewhere which covers how to manipulate textboxes in charts, for example boarders and font changes?

Thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi ziggeh
Welcome to the board

To add a shape to a chart you just add it to its shapes collection. Once you have the reference of the shape, you manipulate as usual.

This is an example. You have a chart object named "Chart 1" in Sheet1. This code adds an oval to the chart, adds text to the oval and does some fomatting to the oval and to the text.

Code:
Sub Macro1()
Dim cht As Chart
Dim shp As Shape
 
    Set cht = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
    
    Set shp = cht.Shapes.AddShape(msoShapeOval, 50, 50, 120, 50)
    
    With shp
        .Name = "Oval 1"

        With .DrawingObject
            .Text = "Some Text"
            With .Font
                .Size = 12
                .Color = RGB(0, 0, 255)
                .Bold = True
            End With
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With
 
        .Fill.ForeColor.RGB = RGB(0, 255, 0)

        With .Line
            .ForeColor.RGB = RGB(255, 0, 0)
            .Weight = 2
        End With
    End With
    
End Sub
 
Upvote 0
Wow thanks a lot, thats really useful.

How do you guys find out these things?

I tried to look at the object browser in "help" but couldn't find this stuff.
 
Upvote 0
I'm glad it helped.

If you look at the Chart object properties you'll see there the Shapes collection. This means that you can add a shape to the chart as you would to a worksheet.

As you have noticed the macro recorder in xl2007 is very bad. It was much more powerful in prior versions. Let's hope this will improve with the SP2.
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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