enter image description hereI wrote a code for my data set, that every time I click on a button, it will switch to the Field list that I selected. But the legend on my chart says Total instead of the Field list name. How do change the legend title? I am pretty new to VBA btw.
Thank you.
ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "2019 Revenue"
This is the code that I used to change the Chart title. I just need to figure out a way to change the legend title now. I tried to use the Macro Recorder for it, but it seems like the regular chart and pivotchart is different.
Sub Add_Value_MasterCode()
Dim pt As PivotTable
Dim pf As PivotField
Dim SField As String
'Set Variable
Set pt = ActiveSheet.PivotTables(1)
SField = ActiveSheet.Shapes(Application.Caller).TextFrame.Characters.Text
'Remove Existing Fields
For Each pf In pt.DataFields
If pf.Name <> "Values" Then
pf.Orientation = xlHidden
End If
Next pf
'Add fields that button was clicked for
pt.PivotFields(SField).Orientation = xlDataField
'Set title and color for each fields
If SField = "2016" Then
ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "2016 Revenue"
With ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
ElseIf SField = "2017" Then
ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "2017 Revenue"
With ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
ElseIf SField = "2018" Then
ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "2018 Revenue"
With ActiveSheet.ChartObjects("chart 1").Chart.FullSeriesCollection(1).Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
ElseIf SField = "2019" Then
ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "2019 Revenue"
With ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent4
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
End If
End Sub