2017-09-15 8 views
0

グラフタイトルと軸タイトルをグラフに表示するためにマクロを作成しようとすると、オンラインで見て、ActiveChart.SetElementとActiveChart.HasTitle = Trueですが、私はどちらかを動かすことができません。私は複数のグラフが一度に作成されているという事実に私の問題があると考えています。私が使用しているコードは以下の通りです:グループ化されたチャートにチャートタイトルと軸タイトルを追加する

Sub Plotting() 

Dim LR As Long 

LR = ActiveSheet.UsedRange.Rows.Count 

Dim aSheet As Worksheet 
For Each aSheet In ActiveWorkbook.Worksheets 'Strain vs Time 
With aSheet.Shapes.AddChart.Chart 
    .ChartType = xlXYScatterSmoothNoMarkers 
    .SetSourceData Source:=aSheet.Range(" '" & aSheet.Name & "'!B3:B15000,'" 
& aSheet.Name & "'!G3:G15000") 
End With 
Next 

For Each aSheet In ActiveWorkbook.Worksheets 'Stress vs Time 
    With aSheet.Shapes.AddChart.Chart 
    .ChartType = xlXYScatterSmoothNoMarkers 
    .SetSourceData Source:=aSheet.Range(" '" & aSheet.Name & "'!B3:B15000,'" 
& aSheet.Name & "'!H3:H15000") 
End With 
Next 

For Each aSheet In ActiveWorkbook.Worksheets 'Stress vs Strain 
With aSheet.Shapes.AddChart.Chart 
    .ChartType = xlXYScatterSmoothNoMarkers 
    .SetSourceData Source:=aSheet.Range(" '" & aSheet.Name & "'!G3:G15000,'" 
& aSheet.Name & "'!H3:H15000") 
End With 
Next 

End Sub 

私は何か助けていただきありがとうございます。

Domenic最初の問題を解決しましたが、現在は作業コードがあります。今私は軸に隣接するY軸のタイトルの向きを変えようとしています。オブジェクトが必要とY軸の名前は行がハイライトさ:私は 『「実行時エラー』 424を取得するコードを実行すると

For Each aSheet In ActiveWorkbook.Worksheets 'Strain vs Time 
    With aSheet.Shapes.AddChart.Chart 
     .ChartType = xlXYScatterSmoothNoMarkers 
     .SetSourceData Source:=aSheet.Range("'" & aSheet.Name & 
"'!B3:B15000,'" & aSheet.Name & "'!G3:G15000") 
    .SetElement msoElementChartTitleAboveChart 
    .SetElement msoElementPrimaryCategoryAxisTitleBelowAxis 
    .SetElement msoElementPrimaryValueAxisTitleAdjacentToAxis 
    .ChartTitle.Text = "MyChartTitle" 'change the chart title as desired 
    .Axes(Type:=xlCategory, AxisGroup:=xlPrimary).AxisTitle.Text = 
"MyCategoryAxisTitle" 'change the category axis title as desired 
    .Axes(Type:=xlValue, AxisGroup:=xlPrimary).AxisTitle.Text = 
"MyValueAxisTitle" 'change the value axis title as desired 
End With 
Next 

:私はこれを試してみました。私が間違っていることについての洞察は?

For Each aSheet In ActiveWorkbook.Worksheets 'Strain vs Time 
    With aSheet.Shapes.AddChart.Chart 
     .ChartType = xlXYScatterSmoothNoMarkers 
     .SetSourceData Source:=aSheet.Range("'" & aSheet.Name & 
"'!B3:B15000,'" & aSheet.Name & "'!G3:G15000") 
    .SetElement msoElementChartTitleAboveChart 
    .SetElement msoElementPrimaryCategoryAxisTitleBelowAxis 
    .SetElement msoElementPrimaryValueAxisTitleHorizontal 
    .ChartTitle.Text = "MyChartTitle" 'change the chart title as desired 
    .Axes(Type:=xlCategory, AxisGroup:=xlPrimary).AxisTitle.Text = 
"MyCategoryAxisTitle" 'change the category axis title as desired 
    .Axes(Type:=xlValue, AxisGroup:=xlPrimary).AxisTitle.Text = 
"MyValueAxisTitle" 'change the value axis title as desired 
End With 
Next 

これは機能しますが、軸のタイトルは水平になります。

これは出力を理想的に見せたいものです。あなたの最初のもののために

Sample Chart

答えて

0

...

For Each aSheet In ActiveWorkbook.Worksheets 'Strain vs Time 
    With aSheet.Shapes.AddChart.Chart 
     .ChartType = xlXYScatterSmoothNoMarkers 
     .SetSourceData Source:=aSheet.Range("'" & aSheet.Name & "'!B3:B15000,'" & aSheet.Name & "'!G3:G15000") 
     .SetElement msoElementChartTitleAboveChart 'change the position as desired 
     .SetElement msoElementPrimaryCategoryAxisTitleBelowAxis 'change the position as desired 
     .SetElement msoElementPrimaryValueAxisTitleVertical 'change the position as desired 
     .ChartTitle.Text = "MyChartTitle" 'change the chart title as desired 
     .Axes(Type:=xlCategory, AxisGroup:=xlPrimary).AxisTitle.Text = "MyCategoryAxisTitle" 'change the category axis title as desired 
     .Axes(Type:=xlValue, AxisGroup:=xlPrimary).AxisTitle.Text = "MyValueAxisTitle" 'change the value axis title as desired 
    End With 
Next 

同じことがあなたの他のもののために適用されるであろう。

希望すると便利です。

+0

それは素晴らしい仕事をした! SetElementsは名前を付ける前に定義する必要がありますか?私は以前と同様のアプローチを試みましたが、なんらかの理由でうまくいきませんでした。 – Cyrus

+0

はい、要素を最初に設定して、対応するオブジェクトを利用できるようにする必要があります。 – Domenic

+0

Y軸ラベルを軸に隣接させる方法はありますか? Iveはこれを試しました: – Cyrus

0

私はにコードを変更してしまった:

Dim LR As Long 

LR = ActiveSheet.UsedRange.Rows.Count 

Dim aSheet As Worksheet 
For Each aSheet In ActiveWorkbook.Worksheets 'Strain vs Time 
With aSheet.Shapes.AddChart.Chart 
    .ChartType = xlXYScatterSmoothNoMarkers 
    .SetSourceData Source:=aSheet.Range("'" & aSheet.Name & "'!B3:B15000,'" & aSheet.Name & "'!G3:G15000") 
    .SetElement msoElementChartTitleAboveChart 'change the position as desired 
    .SetElement msoElementPrimaryCategoryAxisTitleBelowAxis 'change the position as desired 
    .SetElement msoElementPrimaryValueAxisTitleRotated 'change the position as desired 
    .SetElement msoElementLegendNone 
    .ChartTitle.Text = "Strain vs Time" 'change the chart title as desired 
    .Axes(Type:=xlCategory, AxisGroup:=xlPrimary).AxisTitle.Text = "Time (sec)" 'change the category axis title as desired 
    .Axes(Type:=xlValue, AxisGroup:=xlPrimary).AxisTitle.Text = "Strain" 'change the value axis title as desired 
End With 
Next 

そして、それは問題を解決しました。あなたの助けをありがとうDomenic

関連する問題