add title on pie chart using vba

I am trying to add a chart title to a pie chart and would like it to equal the name of the worksheet. having a hard time getting this to work.

I've tried:

Dim ChartTitle As String
ChartTitle = ws.Name
ActiveChart.ChartTitle.Text = "" & Title

Open in new window


I have also tried:

ActiveChart.ChartTitle.Text = ws.Name

Open in new window


Any ideas? Thanks!
eastsidemarketAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harry LeeCommented:
Eastsidemarket,

I have just created the following code for your other question.

Sub CreatePie()
Dim ShapeCnt As Integer, ChtNum As Integer
 Worksheets("test chart page").Select
    Set Rng = Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row)
    Rng.Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xl3DPieExploded
    ShapeCnt = ActiveSheet.Shapes.Count
    For ChtNum = 1 To ShapeCnt
    With ActiveSheet.Shapes(ChtNum).Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorText2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0.6000000238
        .Transparency = 0
        .Solid
    End With
        With ActiveSheet.Shapes(ChtNum).Line
        .Visible = msoTrue
        .Weight = 2
    End With
    ActiveChart.SetElement (msoElementChartTitleCenteredOverlay)
'    ActiveChart.ChartTitle.Text = "test"
    ActiveChart.ChartTitle.Text = ActiveSheet.Name
    Next
End Sub

Open in new window


Adding Chart title works perfectly.

Just wondering if your problem has to do with ActiveChart. With ActiveChart, the chart you want to edit must be selected. The code won't work if you have selected a cell on the sheet instead of the chart.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slubekCommented:
Activechart.HasTitle=True
ActiveChart.ChartTitle.text=ActiveSheet.Name

Open in new window

0
eastsidemarketAuthor Commented:
the title piece of the code does the trick, thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.