2017-09-15 158 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軸標題的方向。我已經試過這樣:

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 

這工作,但軸標題變成水平。

這就是我想要的輸出看起來很理想。

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軸標籤?我試過這個: – 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

相關問題