我有一个问题,试图创建一个宏来将图表标题和轴标题放到我的图表上,我看过网上并尝试使用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
这有效,但Axis标题变为水平。
这就是我希望输出看起来像理想的情况。
答案 0 :(得分: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
同样的事情适用于你的其他事情。
希望这有帮助!
答案 1 :(得分: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