2017-07-30 82 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