繁体   English   中英

Excel图表数据点颜色随过滤器使用情况而变化

[英]Excel chart data point colors change on filter usage

我希望有人可以帮助我解决以下问题。 我使用了3种不同的VBA编码方法(本网站有2种编码方法),所有这些方法都落在下面的同一关卡中。

我有一个电子表格,其中使用2列作为散点图的数据源。 该图表中的数据点基于第三列中使用的条件格式进行着色。 因此,在第3列中,如果值为“ X”,则数据点应为粉红色。 否则,数据点应为绿色。 一切正常。 我是怎么做到的: https : //www.reddit.com/r/excel/comments/3x2cme/scatter_plot_with_color_based_on_a_third_value/这是我的编码:

Sub ColorCode()
    Dim i As Integer
    Dim MyCount As Integer
    MyCount = ActiveChart.SeriesCollection(1).Points.Count * ActiveChart.SeriesCollection.Count
    'MsgBox ActiveChart.SeriesCollection(1).Points.Count * 
    ActiveChart.SeriesCollection.Count
    For i = 1 To MyCount
        ActiveChart.SeriesCollection(1).Points(i).Select
        With Selection.Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = Range("i" & i + 1).DisplayFormat.Interior.Color
        End With
    Next
End Sub

但是,当我过滤源数据时,数据点的颜色会改变。 我已经弄清楚了以下内容:-当未过滤数据源时,第3列中的第四行数据为粉红色。 很好 -过滤数据源时,第3列中的第四行数据为绿色。 很好 -但是,此过滤后的行的相关数据点应为粉红色,而应为绿色-过滤后的源表中的第四行数据将始终保留原始未过滤表的第四行的条件格式。

我需要的是过滤器对数据点的颜色没有影响,但仍然隐藏了过滤掉的数据点。 或者换一种说法,在过滤数据时,数据点将保留其颜色属性,这些颜色属性是上述VBA代码在过滤之前应用的。

数组很方便。

Sub ColorCode()
    Dim i As Integer, n As Integer
    Dim MyCount As Integer
    Dim rngColor As Range, vRng() As Range

    MyCount = ActiveChart.SeriesCollection(1).Points.Count * ActiveChart.SeriesCollection.Count
    Set rngColor = Range("i2", Range("i" & Rows.Count).End(xlUp))
    Set rngColor = rngColor.SpecialCells(xlCellTypeVisible)
    For Each Rng In rngColor
        n = n + 1
        ReDim Preserve vRng(1 To n)
        Set vRng(n) = Rng
    Next Rng

    'MsgBox ActiveChart.SeriesCollection(1).Points.Count *    ActiveChart.SeriesCollection.Count
    For i = 1 To MyCount
        ActiveChart.SeriesCollection(1).Points(i).Select
        With Selection.Format.Fill
            .Visible = msoTrue
            '.ForeColor.RGB = Range("i" & i + 1).DisplayFormat.Interior.Color
            .ForeColor.RGB = vRng(i).DisplayFormat.Interior.Color
        End With
    Next
End Sub

这有助于解决一些类似的图表格式问题:

Excel文件选项卡>选项>高级>图表
取消选中“当前工作簿的属性遵循图表数据点”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM