运行我在Excel 2010中创建的vba宏时出现运行时错误-2147467259 (80004005): Invalid parameter
。当我尝试在设置.CategoryType = xlTimeScale后设置.majorUnitScale = xlMonths时出现错误。尝试使用.chartType = xlLineMarkers
奇怪的是,当我在Excel 2007中运行此代码时,它可以完美地工作并根据需要生成折线图。
以下是代码的一部分:
dim myChtObj as ChartObject
Set myChtObj = ActiveSheet.ChartObjects.Add(Left:=202, Width:=340, Top:=28,Height:=182)
With myChtObj.Chart
' remove extra series
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
.ChartType = xlLineMarkers
.HasTitle = True
.ChartTitle.Text = "Performance Trends"
.ChartTitle.Font.Size = 12
.ChartTitle.Font.Name = "Calibri"
.ChartTitle.Font.FontStyle = "Bold"
With .Axes(xlCategory)
.CategoryType = xlTimeScale
.BaseUnit = xlMonths
.MajorUnit = 2
.MajorUnitScale = xlMonths ' run-time error occurs here
.MinorUnit = 1
.MinorUnitScale = xlMonths
.TickLabels.NumberFormat = "mmm yy"
.TickLabels.Orientation = 45
End With
.....
End with
谢谢!
答案 0 :(得分:0)
Ash -
在您的代码中,尝试在分配轴刻度之前添加系列数据和类别标签。
我可以在图表上成功运行此代码,只要至少有1个系列,并且类别轴标签采用日期格式。
我已将工作簿上传到已创建图表的Google文档。删除图表,但将系列数据保留在B& C列中,然后运行宏AshOriginalWithAddSeries
。我所做的只是添加一系列数据与日期格式XValues
,然后你的代码工作。
https://docs.google.com/file/d/0B1v0s8ldwHRYUWUtRWpqblgzM3M/edit?usp=sharing
Sub AshOriginalWithAddSeries()
Dim cht As Chart
Dim srs As Series
Dim dummyDate As Date
Set cht = ActiveSheet.ChartObjects.Add(Left:=202, Width:=340, Top:=28, Height:=182).Chart
dummyDate = DateSerial(2013, 2, 1)
' remove extra series
With cht
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
'Add at least one series:
Set srs = .SeriesCollection.NewSeries
With srs
.Name = "Series Title"
.Values = 0.5 '=Working!$C$3:$C$14" ' or you can pass an array of stored values
.XValues = Array(dummyDate) '"=Working!$B$3:$B$14" 'or you can pass an array of values, make sure they are valid DATES though
End With
.ChartType = xlLineMarkers
.HasTitle = True
.ChartTitle.Text = "Performance Trends"
.ChartTitle.Font.Size = 12
.ChartTitle.Font.Name = "Calibri"
.ChartTitle.Font.FontStyle = "Bold"
With .Axes(xlCategory)
.CategoryType = xlTimeScale
'.BaseUnit = xlMonths
.MajorUnit = 2
.TickLabels.NumberFormat = "mmm yy"
.TickLabels.Orientation = 45
'.MajorUnitScale = xlMonths ' run-time error occurs here
.MinorUnit = 1
'.MinorUnitScale = xlMonths
End With
End With
'Now assign some real values to srs
With srs
.Name = "Series Title"
.Values = "=Working!$C$3:$C$14" ' or you can pass an array of stored values
.XValues = "=Working!$B$3:$B$14" 'or you can pass an array of values, make sure they are valid DATES though
End With
End Sub
我认为您的代码失败了,因为没有系列数据,因此没有类别值。这是特殊的,因为即使轴不存在,也可以设置轴的某些属性。我注意到了图表自动化中的类似行为 - 有时除非有系列数据,否则不会让您访问属性。
答案 1 :(得分:0)
我有同样的问题。
通过选择包含X轴值的所有单元格,右键单击打开“Format Cells”,然后将Number Category(1st tab)设置为“Date”,我能够解决它。