我正在尝试从表中生成图表。
该表位于我的工作表“测试”中,我希望我的图表位于工作表“状态”中。
我正在使用代码,其中我在行
中收到自动化错误.SetSourceData Source:=rng
我没有每次都收到错误。对于每30次或更多次迭代,我收到此错误。
任何人都可以提供帮助,我如何纠正这个
Sub chart()
Dim rng As Range
Dim cht As Object
Dim Ws As Worksheet
Set Ws = Sheets("Test")
Set rng = Ws.Range("G1:J2")
Set sh = ActiveSheet.ChartObjects.Add(Left:=400, _
Width:=390, _
Top:=100, _
Height:=250)
sh.Select
Set cht = ActiveChart
With cht
.SetSourceData Source:=rng
.ChartType = xlColumnClustered
ActiveChart.FullSeriesCollection(1).Points(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
ActiveChart.FullSeriesCollection(1).Points(2).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
ActiveChart.FullSeriesCollection(1).Points(3).Format.Fill.ForeColor.RGB = RGB(0, 0, 255)
ActiveChart.FullSeriesCollection(1).Points(4).Format.Fill.ForeColor.RGB = RGB(80, 100, 10)
'cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
End With
'cht.SeriesCollection(1).name = ""
cht.SeriesCollection(1).HasDataLabels = True
cht.HasTitle = True
cht.ChartTitle.Text = "Status"
End Sub
答案 0 :(得分:2)
尝试一下:
Sub chart()
Dim rng As Range
Dim cht As Chart
Dim Ws As Worksheet
Set Ws = ThisWorkbook.Sheets("Test")
Set rng = Ws.Range("G1:J2")
Set sh = ThisWorkbook.Sheets("status")
sh.Shapes.AddChart(Left:=400, Top:=100, Width:=390, Height:=250).Select
Set cht = ActiveChart
With cht
.SetSourceData Source:=rng
.ChartType = xlColumnClustered
.SeriesCollection(1).Points(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.SeriesCollection(1).Points(2).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
.SeriesCollection(1).Points(3).Format.Fill.ForeColor.RGB = RGB(0, 0, 255)
.SeriesCollection(1).Points(4).Format.Fill.ForeColor.RGB = RGB(80, 100, 10)
.ChartTitle.Text = "Status"
End With
End Sub