Conditionally format data series based on value

Dear Experts:

A macro should change the formatting for some custom named charts on the active Worksheet as follows:

Data Series that start in 2015 should have a different fill (RGB 230, 185, 184)

All of these charts are bar charts with just one data series

Not all of the charts of the active worksheet have to be formatted this way, but charts with the name 'MyChart_1', 'MyBizChart' and 'ProChart' (and other ones the names of which I will be able to enter into the provided macro code)

conditionally-format-data-series.png
Help is much appreciated. Thank you very much in advance.

I have attached a sample file for your convenience.

Regards, Andreas
Conditionally-format-data-series.xls
Andreas HermleTeam leaderAsked:
Who is Participating?
 
Saurabh Singh TeotiaCommented:
Their you go..Use this code for your first chart...

Sub ColorByValue()
    Dim rng As Range, cell As Range
    Dim iPattern As Long

    Set rng = ActiveSheet.Range("C16:C27")
     iPattern = 1
    For Each cell In rng
       
        If Year(cell.Value) = 2015 Then

            ActiveSheet.ChartObjects("MyChart_1").Select
            With ActiveChart.SeriesCollection(1)
                .Points(iPattern).Format.Fill.ForeColor.RGB = RGB(230, 185, 183)
            End With

        End If
        iPattern = iPattern + 1
    Next cell

End Sub

Open in new window


And use this code for your second chart which is Mybizchart...

Sub ColorByValue2()
    Dim rng As Range, cell As Range
    Dim iPattern As Long

    Set rng = ActiveSheet.Range("C51:C62")
     iPattern = 1
    For Each cell In rng
       
        If Year(cell.Value) = 2015 Then

            ActiveSheet.ChartObjects("MyBizChart").Select
            With ActiveChart.SeriesCollection(1)
                .Points(iPattern).Format.Fill.ForeColor.RGB = RGB(230, 185, 183)
            End With

        End If
        iPattern = iPattern + 1
    Next cell

End Sub

Open in new window


Saurabh...
0
 
Saurabh Singh TeotiaCommented:
Their you go enclosed is your workbook for your reference..where i did what you are looking for but i did without macro..this is conditional formatting only by the way I'm arranging my data and this will do what you are looking for as i tweaked your graphs only...

Saurabh...
Conditionally-format-data-series.xls
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Saurab,

astonishingly nice workaround, thank you very much for it, but to be honest with you, I would favor a macro solution. Maybe you will be able to provide me even a macro code.

Andreas
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Saurabh Singh TeotiaCommented:
Andreas,

Any particular reason you are looking to this only by code??

Saurabh...
0
 
Andreas HermleTeam leaderAuthor Commented:
Actually no specific reason
0
 
Saurabh Singh TeotiaCommented:
The reason i asked this question is because if i provide a macro solution for this i will be reading the range only from which you are preparing the chart and then coloring the series value accordingly...

Let me know if you are fine with the same and will write a macro for you...
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Saurabh, great, this is ok, no problem, thank you very much in advance.

Regards, Andreas
0
 
Saurabh Singh TeotiaCommented:
I'm confused whether you want me to write a macro or you are good with current solution??
0
 
Andreas HermleTeam leaderAuthor Commented:
oh sorry that I confused you, yes I would be very glad if you could write a macro for me.

Regards, Andreas
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Saurabh,

great, works just fine, thank you very much for your professional job. I really appreciate it.

Regards, Andreas
0
 
Saurabh Singh TeotiaCommented:
Yw.. Glad to help.. :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.