【问题标题】:Repetitive Use of Graphs in Multiple Worksheets For Excel(Using Macros)在 Excel 的多个工作表中重复使用图表(使用宏)
【发布时间】:2016-06-23 15:32:50
【问题描述】:

我正在尝试使用宏来编写代码,以帮助我为我现在正在处理的 122 个工作表绘制多个图表。 问题是每个工作表都完全相同,除了要绘制或更改的值。 我已经编写了以下代码,但我无法一次将其应用于多个工作表,并且宏结果特定于它所在的工作表。 请帮忙

Sub ChartMacro()
'
' ChartMacro Macro
'
' Keyboard Shortcut: Ctrl+m
'
 Dim sSheetName As String

    sSheetName = ActiveSheet.Name
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveWindow.SmallScroll Down:=27

    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Name = "=""China"""
    ActiveChart.SeriesCollection(1).Values = "=('&sSheetName&'!$E$11)"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Name = "='sSheetName'!$D$14:$E$14"
    ActiveChart.SeriesCollection(2).Values = "='sSheetName'!$E$20"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(3).Name = "='sSheetName'!$D$23:$E$23"
    ActiveChart.SeriesCollection(3).Values = "='sSheetName'!$E$29"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(4).Name = "='sSheetName'!$D$32:$E$32"
    ActiveChart.SeriesCollection(4).Values = "='sSheetName'!$E$38"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(5).Name = "='sSheetName'!$D$41:$E$41"
    ActiveChart.SeriesCollection(5).Values = "='sSheetName'!$E$47"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(6).Name = "='sSheetName'!$D$50:$E$50"
    ActiveChart.SeriesCollection(6).Values = "='sSheetName'!$E$56"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(7).Name = "='sSheetName'!$D$59:$E$59"
    ActiveChart.SeriesCollection(7).Values = "='sSheetName'!$E$65"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(8).Name = "=""Singapore"""
    ActiveChart.SeriesCollection(8).Values = "='sSheetName'!$E$74"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(9).Name = "='sSheetName'!$D$77:$E$77"
    ActiveChart.SeriesCollection(9).Values = "='sSheetName'!$E$83"
    ActiveChart.SeriesCollection(9).XValues = "='sSheetName'!$A$3:$U$3"

    ActiveChart.ApplyLayout (3)
    ActiveChart.PlotArea.Select
    ActiveChart.Axes(xlCategory).Select
    Selection.TickLabelPosition = xlHigh
    ActiveChart.ChartTitle.Select
    ActiveChart.ChartTitle.Text = "Male-Female"
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Male-Female"

    With Selection.Format.TextFrame2.TextRange.Characters(1, 11).ParagraphFormat
        .TextDirection = msoTextDirectionLeftToRight
        .Alignment = msoAlignCenter
    End With

    With Selection.Format.TextFrame2.TextRange.Characters(1, 11).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 18
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Strike = msoNoStrike
    End With

    ActiveChart.ChartArea.Select
    ActiveChart.SetElement (msoElementDataLabelOutSideEnd)
End Sub

【问题讨论】:

  • 您是否正在尝试确定如何遍历工作簿中的每个工作表?理想情况下每次迭代都替换sSheetName
  • 循环可能不是必需的,但我正在寻找能够为我在其中运行宏的任何活动工作表获取图表。
  • 很难告诉您如何在没有看到数据示例或不知道如何将其应用于多个工作表的情况下概括您的代码。
  • 我无权展示我正在使用的数据。对此感到抱歉。但是有什么办法可以将 sSheetName 用作随活动工作表而变化的变量,因为无论工作表如何,每张工作表的单元格编号都保持不变。

标签: excel macros vba


【解决方案1】:

我会用 Worksheet 对象替换 sSheetName。然后你需要稍微修改你的代码来使用这些新对象。

Sub chartMacro()
'
' ChartMacro Macro
'
' Keyboard Shortcut: Ctrl+m
'
    Dim sheet as Worksheet
    Dim cht as Chart

    Set sheet = ActiveSheet
    Set cht = Charts.Add
    Set cht = cht.Location(Where:=xlLocationAsObject, Name:=sheet.Name)

    cht.ChartType = xlColumnClustered

    cht.SeriesCollection.NewSeries
    cht.SeriesCollection(1).Name = "=""China"""
    cht.SeriesCollection(1).Values = Sheet.Cells(11,5)

    cht.SeriesCollection.NewSeries
    cht.SeriesCollection(2).Name = Sheet.Range("$D$14:$E$14")
    cht.SeriesCollection(2).Values = Sheet.Range("$E$20")
    '...rest of sub follows...

End Sub

您还可以通过循环 SeriesCollection 来稍微清理一下代码,因为看起来您每次将 Sheet.Range() 递增 9 行。

【讨论】:

  • 该错误仍然是 cht=ActiveSheet.Shapes.AddChart 上的类型错误。
  • 很好理解这个概念。成功了,非常感谢您的帮助
  • @ayanbharadwaj 检查我的编辑,我将Dim cht as Shape 更改为Dim cht as Chart。这应该会更好,因为您将处理特定于图表的方法,而不仅仅是形状
猜你喜欢
  • 2016-07-20
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
相关资源
最近更新 更多