我想在Excel中添加默认百分比的图表数据标签。下面是我创建图表的代码:
Private Sub CommandButton2_Click()
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$6:$D$6")
ActiveChart.ChartType = xlDoughnut
End Sub
它只创建没有信息标签的甜甜圈图表。
另外,当我想用同样的信息创建另一个图表类型时,我如何才能改变图表的坐标,使它看起来不像覆盖了同一个图表?
发布于 2015-12-17 01:43:43
下面是在%中使用数据标签的一种方法:
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
https://stackoverflow.com/questions/34330531
复制