Conditional Barchart colour - Help with code !

Raydocks

New Member
Joined
Sep 12, 2003
Messages
7
Hi,

I have created a chart with 2 series. One is a sales value, the other in the target. I have charted this and want each bar to change colour depending on if it is above or below target.

I have calculated the colour indexes using an if statement in cells d2-d7. I have written this code that works a treat for the 1st bar in the series but the others don't change, how do I get this to change all bar colour on recalc.

Thanks,
Ray

Private Sub Worksheet_Calculate()

' Conditional Chart Colour Macro

ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Points(1).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
.ColorIndex = Range("D2:D7")
.Pattern = xlSolid
End With
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Your code is only selecting the fist point. You need to loop around the Points collection like this:

Code:
Sub Test()
    Dim Pt As Point
    Dim Rng As Range
    Dim x As Integer
    Set Rng = ActiveSheet.Range("D2:D7")
    x = 1
    For Each Pt In ActiveSheet.ChartObjects("Chart 2").Chart.SeriesCollection(1).Points
        Pt.Interior.ColorIndex = Range("D2:D7").Cells(x, 1)
        x = x + 1
    Next Pt
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,915
Messages
6,181,724
Members
453,064
Latest member
robatthe2A

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top