enter image description here我为数据集编写了一个代码,每次单击按钮时,它将切换到我选择的“字段”列表。但是我图表上的图例显示总计而不是字段列表名称。如何更改图例标题?我是VBA刚起步的新手。
谢谢。
ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "2019 Revenue"
这是我用来更改图表标题的代码。我只需要找出一种方法来立即更改图例标题。我尝试使用宏记录器,但是看起来常规图表和数据透视图不同。
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
答案 0 :(得分:0)
数据透视图似乎是charts with some mysterious connection to the pivot table。根据文档,ActiveSheet.ChartObjects("Chart 1").Chart.[FullSeriesCollection](1)
returns一个series对象。您可以通过更改其Name
来更改系列对象的图例。因此,我认为在with语句之后,您需要:
ActiveSheet.ChartObjects("Chart 1").Chart.[FullSeriesCollection](1).Name = "field list name"
编辑:我看到@jessi首先到达那里。