Set Legend Text Pie Chart

人盡茶涼 提交于 2020-01-07 05:46:20

问题


I have been trying to set my two legends using a VBA Macro.

I want to change my two legends from 1 and 2 to Male and Female on my Pie Chart.

I can change it manually by setting it in the "Select Data Source" dialog by setting "Category (X) axis labels" to "=Sheet1!$B$6:$C$6" (which is Male and Female)

How do I do this with VBA, though?

Here is my code...

Sub Pie()

    For X = 7 To 13
        Charts.Add
        ActiveChart.ChartType = xlPie
        ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("b" & X & ":c" & X)
        ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
        ActiveChart.HasTitle = True
        CellVal = Worksheets("Sheet1").Range("A" & X).Value

        ActiveChart.ChartTitle.Text = "History statistics of " & CellVal

    Next X

End Sub

回答1:


Pleas change the line with .SetSourceData method into this one:

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("b6:c6," & "b" & x & ":c" & x)


来源:https://stackoverflow.com/questions/18005023/set-legend-text-pie-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!