简体   繁体   English

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

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

I was hoping somebody could help me with the following issue. 我希望有人可以帮助我解决以下问题。 I have used 3 different VBA coding methods (2 from this website) and all of them fall over at the same hurdle below. 我使用了3种不同的VBA编码方法(本网站有2种编码方法),所有这些方法都落在下面的同一关卡中。

I have a spreadsheet that uses 2 columns as the data source for a scatter chart. 我有一个电子表格,其中使用2列作为散点图的数据源。 The data points in this chart are colored based on the conditional formatting used in a 3rd column. 该图表中的数据点基于第三列中使用的条件格式进行着色。 So in column 3, if the value is "X", then the data point should be pink. 因此,在第3列中,如果值为“ X”,则数据点应为粉红色。 Otherwise the data point should be green. 否则,数据点应为绿色。 This is working fine. 一切正常。 How I did this is here: https://www.reddit.com/r/excel/comments/3x2cme/scatter_plot_with_color_based_on_a_third_value/ Here is my coding: 我是怎么做到的: 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

However, when I filter my source data, the colors of the data points change. 但是,当我过滤源数据时,数据点的颜色会改变。 I have figured out the following: - when the data source is unfiltered, the 4th row of data in column 3 is pink. 我已经弄清楚了以下内容:-当未过滤数据源时,第3列中的第四行数据为粉红色。 This is good. 很好 - when the data source is filtered, the 4th row of data in column 3 is green. -过滤数据源时,第3列中的第四行数据为绿色。 This is good. 很好 - however, the related data point for this filtered row is pink, when it should be green - the 4th row of data in the filtered source table will always retain the conditional formatting of the 4th row of the original unfiltered table. -但是,此过滤后的行的相关数据点应为粉红色,而应为绿色-过滤后的源表中的第四行数据将始终保留原始未过滤表的第四行的条件格式。

What I need is for the filter to have no impact on the colors of the data points, yet still hide the data points that are filtered out. 我需要的是过滤器对数据点的颜色没有影响,但仍然隐藏了过滤掉的数据点。 Or, put another way, when filtering the data, the data points are to retain their color properties that the VBA code above applied before filtering. 或者换一种说法,在过滤数据时,数据点将保留其颜色属性,这些颜色属性是上述VBA代码在过滤之前应用的。

Arrays are convenient. 数组很方便。

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

This helps with some similar chart formatting issues: 这有助于解决一些类似的图表格式问题:

Excel File tab > Options > Advanced > Chart Excel文件选项卡>选项>高级>图表
Uncheck 'Properties follow chart data point for current workbook' 取消选中“当前工作簿的属性遵循图表数据点”

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

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