グラフタイトルと軸タイトルをグラフに表示するためにマクロを作成しようとすると、オンラインで見て、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
これは機能しますが、軸のタイトルは水平になります。
これは出力を理想的に見せたいものです。あなたの最初のもののために
それは素晴らしい仕事をした! SetElementsは名前を付ける前に定義する必要がありますか?私は以前と同様のアプローチを試みましたが、なんらかの理由でうまくいきませんでした。 – Cyrus
はい、要素を最初に設定して、対応するオブジェクトを利用できるようにする必要があります。 – Domenic
Y軸ラベルを軸に隣接させる方法はありますか? Iveはこれを試しました: – Cyrus