简体   繁体   中英

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.

I have a spreadsheet that uses 2 columns as the data source for a scatter chart. 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. 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:

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. This is good. - when the data source is filtered, the 4th row of data in column 3 is green. 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.

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
Uncheck 'Properties follow chart data point for current workbook'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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