【问题标题】:Changing chart x axis (category) in VBA - filter blank在 VBA 中更改图表 x 轴(类别) - 过滤空白
【发布时间】:2019-08-16 10:08:46
【问题描述】:

我正在尝试为图表制作一个宏,以根据图表上方的周数(列数)获取更多数据。值完美地工作,但 x 轴(类别)不起作用。

1) 它没有获取新数据 - 尽管 range(*) 本身工作正常

2) 数据在合并单元格中,我需要选择它们,然后过滤那些空(合并)单元格。

Dim StartColumn As Integer
Dim EndColumn As Integer

 Range("XFD3").End(xlToLeft).Select
 StartColumn = Range("XFD3").End(xlToLeft).Column
 EndColumn = Selection.Columns.Count + StartColumn - 1

ActiveSheet.ChartObjects("Graf_THP").Activate
    ActiveChart.FullSeriesCollection(1).Values = Range(Cells(6, 4), Cells(6, EndColumn))
    ActiveChart.FullSeriesCollection(2).Values = Range(Cells(7, 4), Cells(7, EndColumn))
    ActiveChart.FullSeriesCollection(3).Values = Range(Cells(8, 4), Cells(8, EndColumn))
    ActiveChart.FullSeriesCollection(3).XValues = Range(Cells(5, 4), Cells(5, EndColumn))  

' last line is where problems are...

这就是他正在做的事情:

这就是我希望他做的事情:

提前感谢您的帮助

【问题讨论】:

    标签: vba charts filtering axis


    【解决方案1】:

    我建议将XValues 添加到每个SeriesCollections。很难判断它是否适用于您的情况,因为我无法完全理解您所说的整个合并单元格和过滤空单元格的意思,因为我们无权访问您的数据。

    所以我的第一个建议是让你的代码适应这个:

    Dim StartColumn As Integer
    Dim EndColumn As Integer
    
    StartColumn = Range("XFD3").End(xlToLeft).Column
    EndColumn = Range("XFD3").End(xlToLeft).End(xlToRight).Column 'I have no idea what was going on here, 
    'this made more sense to me, reverse this change if I made a wrong assumption.
    
    With ActiveSheet.ChartObjects("Graf_THP")
        .FullSeriesCollection(1).Values = Range(Cells(6, 4), Cells(6, EndColumn))
        .FullSeriesCollection(1).XValues = Range(Cells(5, 4), Cells(5, EndColumn))
        .FullSeriesCollection(2).Values = Range(Cells(7, 4), Cells(7, EndColumn))
        .FullSeriesCollection(2).XValues = Range(Cells(5, 4), Cells(5, EndColumn))
        .FullSeriesCollection(3).Values = Range(Cells(8, 4), Cells(8, EndColumn))
        .FullSeriesCollection(3).XValues = Range(Cells(5, 4), Cells(5, EndColumn))
    End With
    

    看看它是否有效。

    还有另一种方法可能会使事情过于复杂,但我不得不在我的一个项目中使用它,而且它有点简洁。想法是将图表的 x 和 y 值存储为字符串,然后将其作为FullSeriesCollection 传递。它可能有助于更复杂的过滤,所以我也会在下面发布:

    'Imagine having stored the values inside the Variant "arrayOfValues"
    Dim dataX as String, dataY as String
    dataX = "={" & CStr(1)
    dataY = "={" & arrayOfValues.Item(1)
    For j = 2 To arrayOfValues.Count
        dataX = dataX & "," & CStr(j)   'Assuming your x values range from 1 to values count
        dataY = dataY & "," & arrayOfValues(j)
    Next j
    dataX = dataX & "}"
    dataY = dataY & "}"
    
    myChart.FullSeriesCollection(1).XValues = dataX
    myChart.FullSeriesCollection(1).Values = dataY
    

    祝你好运!

    【讨论】:

      【解决方案2】:

      感谢@Lorne 的回复

      • StartColumn, EndColumn = 不知道,在 google 上找到了这个解决方案,它运行顺利(返回选择中最后一列的数量 - 图表数据范围所需的数字)

      • 您的第一个解决方案与我的做法几乎相同

      • 您的第二个解决方案确实过于复杂 - 我根本没有得到它:( :D

      • 合并单元格的问题是这样的:

      https://i.paste.pics/038e7044e4c410caff86a0c05ad3b4a0.png(没有足够的代表发布图片,所以只有链接)

      基本上,我需要做的就是过滤那些“空白”(E2、F2、G2 - 合并单元格)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-23
        • 2016-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-28
        相关资源
        最近更新 更多