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 を使用して 1 つの一連のデータを追加することだけで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 に答える