4

运行我在 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

谢谢!

4

2 回答 2

0

灰 -

在您的代码中,尝试在分配轴刻度之前添加系列数据和类别标签。

只要至少有 1 个系列,并且类别轴标签采用日期格式,我就可以在图表上成功运行此代码。

我已将工作簿上传到已创建图表的 Google 文档。删除图表,但将系列数据保留在 B&C 列中,然后运行宏AshOriginalWithAddSeries。我所做的只是添加一系列带有 date-formatted 的数据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

我认为您的代码失败了,因为没有系列数据,因此没有类别值。它很奇特,因为即使轴不存在也可以设置轴的某些属性。我注意到图表自动化中的其他类似行为 - 有时它不会让您访问属性,除非有系列数据。

于 2013-02-21T01:23:46.257 回答
0

我遇到过同样的问题。

我可以通过选择所有包含 X 轴值的单元格来解决它,右键单击以打开“设置单元格格式”,然后将数字类别(第一个选项卡)设置为“日期”。

于 2018-04-16T14:59:34.083 回答