【发布时间】:2017-09-15 02:20:17
【问题描述】:
我在尝试创建一个宏以将图表标题和轴标题放在我的图表上时遇到问题,我在网上查看并尝试了使用 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
这可行,但轴标题变为水平。
这是我希望输出的理想状态。
【问题讨论】: