昨天在chenjun apolloh两位版主的指导下,我学习了二个不同的方法制作了"线柱图",当我使用代码自动生成该"线柱图",遇到几个问题,还请大家继续赐教:
说明:这里有三个宏,第一个,基本上是录制的宏,我略作修改,该宏运行一直正常(十多次);
第二个宏,我以MYCHART的对象部分进行了调整,运行结果时灵时不灵,有时可以正常运行,有时却是出错,错误代码:ActiveChart.Axes(xlCategory, xlSecondary).CategoryType = xlCategoryScale
错误号:1004(附件中有)
第三个宏,我直接以ADD后的ACTIVECHART进行属性设置,基本不能正确运行,出错代码:
.HasAxis(xlCategory, xlPrimary) = True,错误号:-2147221080
守柔求助的问题是:
如何正确设置代码?如何精简代码?
另外,在 ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="线-柱图"
的代码中,如果ChartType:=xlBuiltIn(自定义的内部图表),如何取得TypeName,或者说如何返加工作表中活动图表的TYPENAME的值,其常量是多少?另外,受版本的影响,该"线-柱图"的英文名称是什么?
我查阅了一下XlChartType常量,其中未有涉及,简单地搜索了一下MSDN也未找到相关资料,还请大家为我释疑,谢谢.
我的版本号见签名.
Option Explicit
Sub Macro2()
Range("A1:M5").Select
Charts.Add
ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="线-柱图"
ActiveChart.SetSourceData Source:=Sheets("Sheet2").Range("A1:M5"), PlotBy:= _
xlRows
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2"
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlCategory, xlSecondary) = False
.HasAxis(xlValue, xlPrimary) = True
.HasAxis(xlValue, xlSecondary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
ActiveChart.Axes(xlCategory, xlSecondary).CategoryType = xlCategoryScale
ActiveChart.HasLegend = True
ActiveChart.Legend.Position = xlBottom
End Sub
'----------------------------------------------------------------------------------------------------------------
Sub Macro3()
Dim MyChart As Chart
' Range("A1:M5").Select
Set MyChart = ActiveWorkbook.Charts.Add
With MyChart
.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="线-柱图"
.SetSourceData Source:=Sheets("Sheet2").Range("A1:M5"), PlotBy:= _
xlRows
.Location Where:=xlLocationAsObject, Name:="Sheet2"
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlCategory, xlSecondary) = False
.HasAxis(xlValue, xlPrimary) = True
.HasAxis(xlValue, xlSecondary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
ActiveChart.Axes(xlCategory, xlSecondary).CategoryType = xlCategoryScale
With ActiveChart
.HasLegend = True
.Legend.Position = xlBottom
End With
End Sub
'----------------------------------------------------------------------------------------------------------------
Sub Macro4()
ActiveWorkbook.Charts.Add
With ActiveChart
.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="线-柱图"
.SetSourceData Source:=Sheets("Sheet2").Range("A1:M5"), PlotBy:= _
xlRows
.Location Where:=xlLocationAsObject, Name:="Sheet2"
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlCategory, xlSecondary) = False
.HasAxis(xlValue, xlPrimary) = True
.HasAxis(xlValue, xlSecondary) = True
.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
.Axes(xlCategory, xlSecondary).CategoryType = xlCategoryScale
.HasLegend = True
.Legend.Position = xlBottom
End With
End Sub
|