MVBで円グラフをマクロにするには?


みっきー  2006-06-14 18:56:25  No: 95756

表から円グラフにするマクロを登録しました。
実行をするとエラーになってしまいます。

エラー内容
実行時エラー'1004';
WorksheetクラスのChartObjectsプロパティを取得できません

デバックを開くと以下の文が出ます。

Sub Macro1()

    Range("G88:H89").Select
    Charts.Add
    ActiveChart.ChartType = xlPie
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("G88:H89"), PlotBy _
        :=xlRows
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2"
    ActiveChart.HasTitle = False
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Points(2).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    With Selection.Interior
        .ColorIndex = 34
        .Pattern = xlSolid
    End With
    ActiveChart.SeriesCollection(1).Points(1).Select
    ActiveChart.ChartArea.Select
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).ApplyDataLabels Type:=xlDataLabelsShowLabel, _
        AutoText:=True, LegendKey:=False, HasLeaderLines:=True
    ActiveChart.SeriesCollection(1).DataLabels.Select
    ActiveChart.SeriesCollection(1).Points(1).DataLabel.Select
    Selection.Left = 174
    Selection.Top = 90
    ActiveChart.SeriesCollection(1).Points(2).DataLabel.Select
    Selection.Left = 117
    Selection.Top = 90
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveChart.SeriesCollection(1).DataLabels.Select
    Selection.AutoScaleFont = True
    With Selection.Font
        .Name = "MS Pゴシック"
        .FontStyle = "太字"
        .Size = 14
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .Background = xlAutomatic
    End With
    ActiveChart.ChartArea.Select
    With Selection.Border
        .Weight = 2
        .LineStyle = 0
    End With
    Selection.Interior.ColorIndex = xlAutomatic
  ☆Sheets("Sheet2").DrawingObjects("グラフ 29").RoundedCorners = False
    Sheets("Sheet2").DrawingObjects("グラフ 29").Shadow = False
    ActiveChart.SeriesCollection(1).DataLabels.Select
    ActiveChart.SeriesCollection(1).Points(2).DataLabel.Select
    Selection.Left = 123
    Selection.Top = 87
    ActiveWindow.Visible = False
    Windows("Book1.xls").Activate
    Range("B20").Select
End Sub

☆印の行に黄色の矢印があります。
実行した時にエラーがでるし、プロットエリアに勝手に背景色がついてるので非常に困っています。
エラーが出ないようにするにはどうしたらいいでしょうか?
またプロットエリアに
こんな説明で申し訳ありませんが、どなたかおわかりになる方教えてください。


特攻隊長まるるう  2006-06-21 00:19:51  No: 95757

MVB というのがよく分からないけど、(^^;)

[Excel VBA]で回答しておきます。
まず、[マクロの記録]を使用すると、Active〜 とか Selection
を多用したコードが記録されますが、これをそのまま使用
すると、不具合を起こし易くなります。

    Dim TagetChart As Chart
    Set TagetChart = ThisWorkbook.Charts.Add
    With TagetChart
        .ChartType = xlPie
        .SetSourceData Source:=ThisWorkbook.Sheets("Sheet1").Range("G88:H89"), _
                       PlotBy:=xlRows
    End With
といった、対象のオブジェクトを明示する形に書き直しましょう。

>  ☆Sheets("Sheet2").DrawingObjects("グラフ 29").RoundedCorners = False
DrawingObjects ???エラーメッセージ通り ChartObjects だったとして、
『グラフ 29』という名前の ChartObjects が存在しないためだと思います。

ワークシート上にある埋め込みグラフは ChartObject オブジェクトという
コンテナに格納されますが、新しくシートに移動したグラフのコンテナは、
Parent プロパティで取得できるようです。
 # 他にコンテナを取得する適当な手段は見つけられませんでした。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加