制作饼图3D并显示百分比

时间:2018-04-19 15:33:04

标签: vba excel-vba excel

我创建了这个简单的宏来形成一些数据的饼图,但我希望它能显示百分比,最好是3D。有谁知道如何调整这个?

Sub Pie_Chart()

Dim Sh As Worksheet
Dim chrt As Chart

Set chrt = Nothing
Set Sh = ActiveWorkbook.Worksheets("Graphs")
Set chrt = Sh.Shapes.AddChart.Chart
With chrt

        'Data
        .ChartType = xlPie

        'Titles
        .HasTitle = True
        .ChartTitle.Characters.Text = Cells(i + 12, 2).Value
        .Hasvalues = True

        'legend
        .HasLegend = True

    End With


    End Sub

2 个答案:

答案 0 :(得分:0)

试试这个:

Sub Pie_Chart()

Dim Sh As Worksheet
Dim chrt As Chart

Set chrt = Nothing
Set Sh = ActiveWorkbook.Worksheets("Graphs")
Set chrt = Sh.Shapes.AddChart.Chart
With chrt

        'Data
        .ChartType = xl3DPie

        'Titles
        .HasTitle = True
        .ChartTitle.Characters.Text = Cells(i + 12, 2).Value
        .Hasvalues = True

        'Legend
        .HasLegend = True

        'Style
        .ChartStyle = 264
End With

End Sub

答案 1 :(得分:0)

或者

Option Explicit

Public Sub Pie_Chart()

    Dim Sh As Worksheet, chrt As Chart

    Set Sh = ActiveWorkbook.Worksheets("Graphs")
    Set chrt = Sh.Shapes.AddChart2(262, xl3DPie).Chart
    With chrt
        .SetSourceData Source:=Range("Graphs!$A$1:$A$4") 'add some data
        .HasTitle = True
       ' .ChartTitle.Characters.Text = Cells(i + 12, 2).Value 'what is i?
        .HasLegend = True
        With .SeriesCollection(1)
            .ApplyDataLabels
            .DataLabels.ShowPercentage = True
            .DataLabels.ShowValue = False   
        End With
    End With
End Sub