【问题标题】:Adding Chart title and Axis titles to Grouped Charts将图表标题和轴标题添加到分组图表
【发布时间】: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 

这可行,但轴标题变为水平。

这是我希望输出的理想状态。

Sample Chart

【问题讨论】:

    标签: vba excel charts


    【解决方案1】:

    对于你的第一个...

    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
    

    同样的事情也适用于你的其他人。

    希望这会有所帮助!

    【讨论】:

    • 效果很好! SetElements 是否必须先定义才能命名?我之前尝试过类似的方法,但由于某种原因,它不起作用。
    • 是的,您需要先设置元素,以便它们对应的对象可用。
    • 任何想法如何使 Y 轴标签与轴相邻?我试过这个:
    • 你试过什么?它应该位于轴旁边。也许你不希望文本垂直?也许你想要msoElementPrimaryValueAxisTitleBelowAxis
    • 我编辑了上面的帖子以显示我所做的以及我想要输出的内容。
    【解决方案2】:

    我最终将代码更改为:

    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

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      相关资源
      最近更新 更多