2017-07-30 109 views
2

我正在从数据透视表中的可用数据生成图表。在图表中添加辅助轴

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

数据透视表包含百分比值和绝对值。我有列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 

任何铅将是有益的

+1

你记录了一个宏,你在其中添加次轴?这将(取决于Excel版本)生成可以探索和借鉴的代码。 –

+0

cht.SeriesCollection(3).AxisGroup = 2 –

回答

1

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

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

编辑1:对于今后的文章中,使用此代码(这是因为它没有使用任何ActiveSheet,Select,ActiveChartSelection

此外,请尝试始终使用Option Explicit并提前定义所有变量和对象。 d,电子柱AR的

代码

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 
+0

我使用了相同的代码,这个修改我的主要Y轴的百分比,并没有第二轴被添加 – Jenny

+0

你能帮助我吗? – Jenny

+0

使用其他提交。在添加轴之前,您需要将一个或多个系列分配给辅助轴组,并且事实上,一旦将任何系列分配给辅助轴组,系统就会默认添加辅助数值轴。 –

1

seriescollections由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