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!
Who is Participating?
Harry LeeConnect With a Mentor Commented:

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)
    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
    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
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.

Open in new window

eastsidemarketAuthor Commented:
the title piece of the code does the trick, thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.