vba - 在图表中添加辅助轴

标签 vba excel

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

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

数据透视表具有百分比值和绝对值。我在 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:对于以后的帖子,请使用此代码(它是您的),因为它不使用任何 ActiveSheetSelect ActiveChart选择

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

代码

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

关于vba - 在图表中添加辅助轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45398696/

相关文章:

vba - LEN() 在 VBA 中返回错误值

VBA获取单击超链接的单元格的值

excel - VBA 中的日期/时间比较

excel - COUNTIFS 公式 - 唯一值

自动填充单元格的 VBA 宏

excel - 如何在vba中的另一个函数中使用一个函数的变量?

excel - 舍入值未正确插入 Excel

Excel提取文本中的粗体字

vba - 保存当前日期的宏

excel - 如何在 VBA 中获取行范围 2 :60 and column range D:H 中单元格的最大值