【问题标题】:VBA to change single / specific series Y values range based on column for multiple chartsVBA根据多个图表的列更改单个/特定系列Y值范围
【发布时间】:2019-09-02 00:14:43
【问题描述】:

我对 VBA 还很陌生。我在 excel 的一个选项卡上有 100 多个图表,其中包含多个数据系列。为了创建这些,我简单地构建了一个图表并手动复制到每一列中。除了一个(四个)数据系列的数据范围外,每个图表都是相同的。仅对于这些系列,我想更改每个图表的 YValues 以引用下一列(因此图表 1 Y 值范围是 A4:A100,图表 2 是 B4:B100,图表 3 是 C4:C100,依此类推) .

我正在考虑一个循环来选择每个图表,选择有问题的系列,并调整 Y 范围。

类似(不起作用),每次将范围移动到下一列时,我都会增加 1

ActiveChart.SeriesCollection(1).YValues = RangeRange(Cells(4, i), Cells(100, i))

但是,这似乎只适用于 Xvalues - 似乎没有等效的 Yvalue 属性。

ActiveChart.SeriesCollection(1).XValues = RangeRange(Cells(4, i), Cells(100, i))

听起来很简单,但在互联网上进行了广泛搜索,但找不到如何仅在整个图表数据范围内编辑图表上的特定数据系列范围。感谢任何帮助。

谢谢

【问题讨论】:

    标签: excel vba charts


    【解决方案1】:

    不是YValues,而是Values...

          ActiveChart.SeriesCollection(1).Values = Range(Cells(4, i), Cells(100, i))
    

    【讨论】:

    • 另外,它是Range,而不是RangeRange
    • 谢谢你们。这行得通。想分享我的最终代码,但这里似乎受到评论中字符数的限制
    【解决方案2】:

    我的最终代码: 通过复制模板图表创建任意数量的图表 将新图表并排放置 根据列引用设置新的图表标题 调整指定图表系列的范围

    子图表生成器()

    Dim i As Integer
    Dim cht As ChartObject
    Dim dChart As Object
    Dim NumCharts As Integer
    Dim myRange As Range
    
    
    Set myRange = ActiveSheet.Range("G3:DD3")
    
    NumCharts = Application.WorksheetFunction.CountA(myRange)
    
    For i = 1 To NumCharts
    
    ' copy template chart called "charttemplate"
     Set dChart = ActiveSheet.ChartObjects("charttemplate").Duplicate
     dChart.Select
    
    ' place it correctly
    dChart.Top = ActiveSheet.ChartObjects("charttemplate").Top + ActiveSheet.ChartObjects("charttemplate").Height + 10
    dChart.Left = (i - 1) * dChart.Width + ActiveSheet.ChartObjects("charttemplate").Left
    
    ' set chart title
    dChart.Name = "newchart" & "" & i
    dChart.Chart.HasTitle = True
    dChart.Chart.ChartTitle.Text = "='" & ActiveSheet.Name & "'!R3C" & i + 6
    
    ' adjust series
    ActiveChart.SeriesCollection(1).Values = Range(Cells(4, i + 6), Cells(10000, i + 6))
    
    Next i
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多