在图表中添加辅助轴

我正在从数据透视表中可用的数据生成图表。

我想从数据透视表中生成一个柱状图。

数据透视表具有百分比值和绝对数字。 我有D和E列的百分比值,B和C列的绝对数字。我想为我的图表有一个次要的y轴百分比。 任何人都可以告诉我如何可以继续这个?

我已经进行了如下所示的代码。

Sub charts () Dim cht As Chart 'ThisWorkbook.Sheets("Status").ChartObjects.delete If ActiveSheet.PivotTables.Count = 0 Then Exit Sub Set ptable = ActiveSheet.PivotTables(1) Set ptr = ptable.TableRange1 Set Sh = ActiveSheet.ChartObjects.Add(Left:=1, _ Width:=390, _ Top:=100, _ Height:=250) Sh.Select Set cht = ActiveChart With cht .SetSourceData ptr .ChartType = xlColumnClustered End With 'cht.SeriesCollection(2).Axes(xlValues, xlSecondary).MaximumScale = 10 cht.SeriesCollection(1).HasDataLabels = True cht.SeriesCollection(2).HasDataLabels = True cht.SeriesCollection(3).HasDataLabels = True cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(0, 255, 0) '<~~ Red cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0) cht.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 0, 255) cht.HasTitle = True cht.ChartTitle.Text = "Status" End Sub 

任何领导都会有所帮助

在设置cht对象后添加以下代码:

 With cht .HasAxis(xlValue, xlSecondary) = True ' add the secondary axis .Axes(xlSecondary).TickLabels.NumberFormat = "0.0%" ' format it to percentage End With 

编辑1 :为将来的职位,使用此代码(这是你的),因为它不使用任何ActiveSheetSelectActiveChartSelection

此外,尝试始终使用Option Explicit并提前定义所有variables和对象。

 Option Explicit Sub charts() Dim ChtObj As ChartObject Dim Sht As Worksheet Dim PvtTbl As PivotTable Dim PvtRng As Range ' first set the sheet object Set Sht = ThisWorkbook.Worksheets("Sheet1") '<-- modify to your sheet's name If Sht.PivotTables.Count = 0 Then Exit Sub ' set the Pivot Table Set PvtTbl = Sht.PivotTables(1) ' set the Range of the Chart (from the Pivot Table's range) Set PvtRng = PvtTbl.TableRange1 ' set the Chart Object Set ChtObj = Sht.ChartObjects.Add(Left:=1, Width:=390, _ Top:=100, Height:=250) ' modify the Chart Object's properties With ChtObj.Chart .SetSourceData PvtRng .ChartType = xlColumnClustered 'cht.SeriesCollection(2).Axes(xlValues, xlSecondary).MaximumScale = 10 ' add series to chart and format them .SeriesCollection(1).HasDataLabels = True .SeriesCollection(2).HasDataLabels = True .SeriesCollection(3).HasDataLabels = True .SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(0, 255, 0) '<~~ Red .SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0) .SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 0, 255) ' add title .HasTitle = True .ChartTitle.Text = "Status" ' add a secondary axis, and format it .HasAxis(xlValue, xlSecondary) = True ' add the secondary axis .Axes(xlSecondary).TickLabels.NumberFormat = "0.0%" ' format it to percentage End With End Sub 

d,e列的序列集合由AxisGroup = 2进行分组。

 Sub charts() Dim cht As Chart Dim ptable As PivotTable Dim ptr As Range Dim Sh As ChartObject 'ThisWorkbook.Sheets("Status").ChartObjects.delete If ActiveSheet.PivotTables.Count = 0 Then Exit Sub Set ptable = ActiveSheet.PivotTables(1) Set ptr = ptable.TableRange1 Set Sh = ActiveSheet.ChartObjects.Add(Left:=1, _ Width:=390, _ Top:=100, _ Height:=250) Set cht = Sh.Chart With cht .SetSourceData ptr .ChartType = xlColumnClustered End With 'cht.SeriesCollection(2).Axes(xlValues, xlSecondary).MaximumScale = 10 cht.SeriesCollection(1).HasDataLabels = True cht.SeriesCollection(2).HasDataLabels = True 'cht.SeriesCollection(3).HasDataLabels = True cht.SeriesCollection(3).AxisGroup = 2 cht.SeriesCollection(4).AxisGroup = 2 cht.SeriesCollection(3).ChartType = xlLine cht.SeriesCollection(4).ChartType = xlLine cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(0, 255, 0) '<~~ Red cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0) cht.SeriesCollection(3).Format.Line.ForeColor.RGB = RGB(0, 0, 255) cht.SeriesCollection(4).Format.Line.ForeColor.RGB = RGB(110, 110, 255) cht.HasTitle = True cht.ChartTitle.Text = "Status" cht.Axes(xlValue, xlSecondary).TickLabels.NumberFormat = "0%" End Sub