我有一個問題,試圖創建一個宏來將圖表標題和軸標題放到我的圖表上,我在線查看並嘗試了使用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軸標題的方向。我已經試過這樣:
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
當我運行的代碼我得到「運行時錯誤‘424’:所需的對象,其中,Y軸被命名爲被突出顯示的行。任何有關我在做什麼錯誤的見解?
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軸標籤?我試過這個: – Cyrus