【问题标题】:How can I add chart data labels with percentage?如何添加带有百分比的图表数据标签?
【发布时间】:2015-12-17 23:15:34
【问题描述】:

我想用 Excel VBA 默认添加带有百分比的图表数据标签。这是我创建图表的代码:

Private Sub CommandButton2_Click()
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$6:$D$6")
ActiveChart.ChartType = xlDoughnut
End Sub  

它只创建没有信息标签的甜甜圈图。

另外,当我想使用相同的信息创建另一种图表类型时,如何更改图表的坐标,使其看起来不会覆盖同一个图表?

【问题讨论】:

    标签: excel vba charts


    【解决方案1】:

    这是在 % 中使用数据标签的一种方法:

                Private Sub CommandButton2_Click()
    Dim Cell As Range
        Set Cell = ActiveCell
                Set Myrange = Sheets("Sheet1").Range("$A$6:$D$6")
    
                ActiveSheet.Shapes.AddChart.Select
                ActiveChart.SetSourceData Source:=Myrange
                ActiveChart.ChartType = xlDoughnut
    
                With PlotArea
                    ActiveChart.ApplyLayout (6)
                End With
    
                With ActiveChart
                    .Legend.Delete
                    '.ChartTitle.Delete
                    '.ChartTitle.Text = "Here goes your tittle"
                End With
    
            With ActiveChart.SeriesCollection(1)
                .Points(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
                .Points(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
                .Points(3).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
                .Points(4).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
            End With
    
        With ActiveChart.Parent
                 .Height = 200 ' resize
                 .Width = 300  ' resize
                 .Top = Cell.Top    ' reposition
                 .Left = Cell.Left  ' reposition
             End With
    
                End Sub
    

    第二种图表:

    Private Sub CommandButton2_Click()
        Set MyRange = Sheets("Sheet1").Range("$A$6:$D$6")
    
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=MyRange
        ActiveChart.ChartType = xlColumnClustered
    
        With PlotArea
            ActiveChart.ApplyLayout (2)
        End With
    
        With ActiveChart
            .Legend.Delete
            '.ChartTitle.Delete
            '.ChartTitle.Text = "Here goes your tittle"
        End With
    
    With ActiveChart.SeriesCollection(1)
        .Points(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Points(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Points(3).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Points(4).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
    End With
    
    With ActiveChart.Parent
         .Height = 200 ' resize
         .Width = 300  ' resize
         .Top = 300    ' reposition
         .Left = 300   ' reposition
     End With
    
    End Sub
    

    您可以在这里找到颜色代码: http://dmcritchie.mvps.org/excel/colors.htm

    【讨论】:

    • 我在百分比数字 1,2,3 上看到了 - 我如何才能删除它?我也可以更改默认颜色 @manu 吗?
    • 查看我的编辑,我也为标题添加了一行
    • 是的,很酷,它正在工作。从默认@manu 设置颜色怎么样?
    • 我有办法让图表的颜色默认为我需要的颜色吗?
    • @Anahit DEV 看到我的编辑,这里的图表都是红色的,在我放的链接中你可以找到颜色代码。
    猜你喜欢
    • 2021-09-12
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多