【问题标题】:Generating dynamic charts with VBA使用 VBA 生成动态图表
【发布时间】:2016-01-21 12:50:20
【问题描述】:

我必须创建近 200 个时间序列图表。所以我尝试编写一个宏来完成我需要做的大部分工作。
我以这样的时间序列生成名称为例:

Name:= AKB_ExampleA

名称指的是我用这个公式声明的动态范围:

=OFFSET('sheet1'!$C$7:$C$137;0;0;COUNT('sheet1'!$C$7:$C$206))

现在到我编写的宏:

Sub graphik_erstellen()

Call graphik1("AKB")

End Sub

Sub graphik(Name As String)
'
    Dim Ch As Chart
    Dim RngToCover As Range
    Set Ch = charts.Add
    Set Ch = Ch.Location(Where:=xlLocationAsObject, Name:="Charts")

    With Ch
      .ChartType = xlLine
      .SetSourceData Source:=Range(Name & "_ExampleA")
      .SeriesCollection(1).XValues = Range("Datum_Volumen")
      .SeriesCollection(1).Name = "SERIES1"
      .FullSeriesCollection(1).Select
        With Selection.Format.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(192, 0, 0)
            .Transparency = 0
        End With
      .HasTitle = True
      .ChartTitle.Text = Name & ", Volumen (nach Korrektur)"
      .HasLegend = True
      .Legend.Position = xlLegendPositionBottom
      .Legend.Select
        Selection.Format.TextFrame2.TextRange.Font.Size = 11
        Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue
      With .Parent
        .top = 100
        .left = 100
        .height = 287.149606299
        .width = 543.685039370078
        .Name = Name & "_chart"
      End With
End With

End Sub

我的问题是,如果我这样做,则不会真正考虑动态范围。它采用名称的范围(即 $C$7:$C$137),但它应该引用名称本身(以便动态)。
因此,如果我单击图表查看系列,系列值将声明为:='sheet1'!$C$7:$C$137 而不是 ='sheet1'!ExampleA

如果有人能帮助我,我会非常非常感激。 最好的 埃利奥

【问题讨论】:

  • 欢迎来到 StackOverFlow。你真的应该使用Range 设置你的Range。只是为了确认您希望捕获到图表中的工作表上的范围是多少?
  • 添加了段落并修正了一些语法
  • 感谢你们到目前为止的cmets。 @Jean-Pierre Oosthuizen 所以我的范围需要是动态的。起价为 7 加元,目前为 137 加元。每个月都会添加新数据,因此图表应该会自动调整。
  • 我看到你使用的是 Office 2010 还是 360 FullSeriesCollection
  • 这是 Office 360​​

标签: vba dynamic charts


【解决方案1】:

我重新排列了几行代码,并尝试放置 cmets 来引用它们。

让我知道什么有效。您可能需要将SeriesCollection 更改为FullSeriesCollection。除此之外,该代码适用于我的 Excel 2010。

第一个Sub我只是根据Column“C”中可用的数据从Row 7得到Range的大小。

告诉我。

Option Explicit

Sub graphik_erstellen()

    'You always want to use direct reference to a sheet/range chart
    'Refering to the WorkBook they are in and the worksheet as well.
    'especially if you are opening multiple WorkBooks / Sheets
    Dim CurrentWorkSheet As Worksheet
    Set CurrentWorkSheet = Workbooks("Book1").Worksheets("Sheet1")

    'Dynamically finding the end of the data in Column C
    Dim LastRow As Long
    LastRow = CurrentWorkSheet.Cells(CurrentWorkSheet.Rows.Count, "C").End(xlUp).Row

    'Setting the range using the document reference aswell
    Dim AKB As Range
    Set AKB = Workbooks("Book1").Worksheets("Sheet1").Range(Cells(7, "C"), Cells(LastRow, "C"))


    Call graphik(AKB)

End Sub

Sub graphik(Name As Range)

    Dim DataChart As Chart
    Dim RngToCover As Range
    Set DataChart = Workbooks("Book1").Charts.Add

    'With Excel 2010 the line above will automatically add the chart as a sheet and not aobject in a sheet
    'Set DataChart = DataChart.Location(Where:=xlLocationAsObject, Name:="Charts")


    With DataChart
        .Name = "Charts" ' This will be the Name of the CHart Tab
        .ChartType = xlLine
        .SetSourceData Source:=Name

        'You can see below I avoided the Select and Selection
        With .SeriesCollection(1)
            'Using Offset I just used the data one cell to the left of the range
            .XValues = Name.Offset(0, -1)
            .Name = "SERIES1"
            With .Format.Line
                .Visible = msoTrue
                .ForeColor.RGB = RGB(192, 0, 0)
                .Transparency = 0
            End With
        End With

        .HasTitle = True
        .ChartTitle.Text = "MIDDEL TOP TEXT" 'Name & ", Volumen (nach Korrektur)"
        .HasLegend = True
        With .Legend
            .Position = xlLegendPositionBottom
            .Format.TextFrame2.TextRange.Font.Size = 11
            .Format.TextFrame2.TextRange.Font.Bold = msoTrue
        End With

        'Not sure about this, it doesnt work in my Excel 2010
        '
        With .Parent
            .Top = 100
            .Left = 100
            .Height = 287.149606299
            .Width = 543.685039370078
            .Name = Name & "_chart"
        End With

    End With

End Sub

让我知道您对工作表和图表名称的意图,然后我可以帮助您获得所需的内容。

【讨论】:

  • 亲爱的让-皮埃尔。非常感谢您的专业回复!我刚刚调整了工作表名称,就是这样,它可以工作:)!我真的很感谢你的帮助!!希望你有一个美好的一周,再次感谢
  • 很高兴您得到了所需的帮助。记得给你觉得有用的答案投票。
  • 我的声誉仍然很低,所以我无法投票。我会尽快的。
  • 啊,是的,忘记了。享受
猜你喜欢
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 2014-09-14
相关资源
最近更新 更多