2013-07-22 53 views
1

我在获取Run Time error 1004 :Method name of object _chart failed的同时尝试命名我的图表。有时会运行,有时却不会。下面是导致该错误的部分,(整个宏是很长,所以我还没有发布这一切。)对象_chart失败的方法名称

绘制直方图的3D山坳图表:

Range("U1:R23").Select 
    Charts.Add 
    ActiveChart.ChartType = xlCylinderCol 
    ActiveChart.Name = "mcChart" -----------> Error 
With ActiveChart 
.HasTitle = True 
.ChartTitle.Characters.Text = "Dynamic Weight Chasing Histogram" & Chr(10) & "Local SWT ,File:" & Name & ", Roadforce-Check Spin" & Chr(10) & "assembly after dimensions changed, Old software" 
.Axes(xlCategory, xlPrimary).HasTitle = True 
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "weights(Oz)" 
Charts("mcChart").SeriesCollection(1).XValues = Array(0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, "4-5", "5-6", "6-7", "7-8", "8-9", "9-10") 
.SeriesCollection([4]).Interior.Color = RGB(139, 0, 0) ' dark red 
.SeriesCollection([3]).Interior.Color = RGB(205, 92, 92) 'indian red 
.SeriesCollection([2]).Interior.Color = RGB(128, 0, 128) 'magenta 
.SeriesCollection([1]).Interior.Color = RGB(144, 238, 144) 'light green 
End With 

请让我知道你是否能找到解决这个问题的办法。

回答

0

你可以修改你的代码,看看它是否工作?

dim newChart as Chart 

Range("U1:R23").Select 
Set newChart = Charts.Add 

With newChart 
    .ChartType = xlCylinderCol 
    .Name = "mcChart" & Format(now, "hhmmss") 

    'setting other properties 
End With 
+0

这样做会给我另一个运行时错误1004:“无法将工作表重新命名为另一个工作表,参考对象库或工作簿参考”。我确信我的工作簿中没有名为mcChart的工作表。 –

+1

设置'.Name =“mcChart”&Format(Now,“hhmmss”)'让我知道它是否失败。我的猜测是,代码是添加图表(当宏运行时),并试图给每个图表使用相同的名称,这可能会失败。图表互相重叠,因此您无法看到当前图表背后的图表。 – shahkalpesh

+0

此代码添加图表*工作表*,它需要唯一的名称,如任何工作表对象,因此您的怀疑已得到确认。如果工作表名称已存在,则会引发1004错误。 **注意:**普通的'ChartObject.Chart'不需要唯一的名称。 –