Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Positive and Negative Colors in a Chart.

Positive and Negative Colors in a Chart

by Allen Wyatt
(last updated March 20, 2017)

7

Merril asked if there was a way to create a line chart so that when a line represented a negative value, the color of the line would change at the point when it went negative. For instance, in a particular data series, as long as the line represented positive values, it would be blue, but when the line represented negative values, it would change to red.

Unfortunately there is no way to easily do this in Excel. There are, however, a couple of workarounds you can try. The first is to use a macro to change the line colors of chart lines that represent negative values. The following macro is an example of such an approach:

Sub PosNegLine()
    Dim chtSeries As Series
    Dim SeriesNum As Integer
    Dim SeriesColor As Integer
    Dim MyChart As Chart
    Dim R As Range
    Dim i As Integer
    Dim LineColor As Integer
    Dim PosColor As Integer
    Dim NegColor As Integer
    Dim LastPtColor As Integer
    Dim CurrPtColor As Integer

    PosColor = 4 'Green
    NegColor = 3 'red
    SeriesNum = 1

    Set MyChart = ActiveSheet.ChartObjects(1).Chart
    Set chtSeries = MyChart.SeriesCollection(SeriesNum)
    Set R = GetChartRange(MyChart, 1, "Values")

    For i = 2 To R.Cells.Count
        LastPtColor = IIf(R.Cells(i - 1).Value < 0, NegColor, PosColor)
        CurrPtColor = IIf(R.Cells(i).Value < 0, NegColor, PosColor)

        If LastPtColor = CurrPtColor Then
            LineColor = LastPtColor
        Else
            If Abs(R.Cells(i - 1).Value) > Abs(R.Cells(i).Value) Then
                LineColor = LastPtColor
            Else
                LineColor = CurrPtColor
            End If
        End If
        chtSeries.Points(i).Border.ColorIndex = LineColor
    Next i
End Sub
Function GetChartRange(Ch As Chart, Ser As Integer, _
  ValXorY As String) As Range
    Dim SeriesFormula As String
    Dim ListSep As String * 1
    Dim Pos As Integer
    Dim LSeps() As Integer
    Dim Txt As String
    Dim i As Integer

    Set GetChartRange = Nothing

    On Error Resume Next
        SeriesFormula = Ch.SeriesCollection(Ser).Formula
    ListSep = ","
    For i = 1 To Len(SeriesFormula)
        If Mid$(SeriesFormula, i, 1) = ListSep Then
            Pos = Pos + 1
            ReDim Preserve LSeps(Pos)
            LSeps(Pos) = i
        End If
    Next i

    If UCase(ValXorY) = "XVALUES" Then
        Txt = Mid$(SeriesFormula, LSeps(1) + 1, LSeps(2) - LSeps(1) - 1)

        Set GetChartRange = Range(Txt)
    End If

    If UCase(ValXorY) = "VALUES" Then
        Txt = Mid$(SeriesFormula, LSeps(2) + 1, LSeps(3) - LSeps(2) - 1)

        Set GetChartRange = Range(Txt)
    End If
End Function

When you select a chart and then run the PosNegLine macro, it looks through the chart and, for line segments between negative data point values, changes the line color to red. For line segments connecting positive data points, the line color is set to green.

The problem with this solution is that it provides only an approximation; it only works with lines connecting two data points, and it can either change the entire line segment or not. If the beginning data point is positive and the ending data point is negative, it cannot change the color of a line right as it passes into negative values.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1796) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Positive and Negative Colors in a Chart.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Having Numbered Lists in Subdocuments Restart their Numbering

If you have a bunch of subdocuments, and each of those subdocuments contains numbered lists, you may find that you have ...

Discover More

Closing a Read-Only Workbook

When you create a workbook that is read-only, users can still make changes to the workbook. When they exit, they are prompted ...

Discover More

Adding a Macro to a Toolbar

A great way to customize Excel is to add your macros to a toolbar. That way you can run them quickly and easily.

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

MORE EXCELTIPS (RIBBON)

Creating Sparklines

Want a cool, small chart to show what your data is doing? You need a sparkline, discussed in this tip.

Discover More

Adjusting Trendlines in a Chart

In the past, Excel allowed you to actually adjust lines and data points in a chart and have your adjustments reflected in the ...

Discover More

Make that Chart Quickly!

Need to generate a chart in the fastest possible way? Just use this shortcut key and you'll have one faster than you can yell ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 8?

2017-03-21 15:08:36

Neil

Nice Yoann! The 49%-51% split only works perfectly if half of your data is above zero. If more than half is positive or negative, you'll need to adjust the split accordingly to something like 60%-61% or 43%-44% for instance. For bar charts, Excel has the "invert when negative" feature.


2013-03-13 06:45:05

Joe Marten

Hi Yoann,
Pretty darn slick!


2013-03-12 20:13:31

Yoann

Hi,
I'm using Excel 2010 and I've managed to do it using a simple trick: in the Chart tools, go to the Format tab, then Fill, then choose a gradient. Put a color for the position 0%, the same color at 49%, then another color for 51% and this color again for 100%. This way the color changes when the value become positive! :)


2013-03-06 09:42:11

Shandor

Barry's technique seems the most efficient. But the original situation is dubious; why characterize negs in a line chart by color alone, when negs are best perceived when they dip below a horizon, as in bar charts where positives are above the line & negs below, even if colored. A horizontal line could pass through the line series to offer the transition point. But maybe this is a special case.


2013-03-06 08:24:51

Dave

Interestingly I ran the code on a simply 24 month set of data. Initially the chart had a legend for 1 data series, and after the code the legend is expanded to 24 data series. Not the intended result.


2013-03-06 08:13:47

Dave

I agree with Barry, add this complexity of a macro is over kill when it can be done with different sets of data.

This tehcnique with different set of data also works for stacked area charts.


2013-03-06 08:00:59

Barry Fitzpatrick

I've have successfully achieved this without the use of macros.

Create from the original data two data series one for the positive values, and the other for the negative values making sure that the negative values in the positive series are blank, and positive values in the negative series are blank. (This is because blanks will not be plotted in a chart). You need to decide how to handle the transition in order to avoid a discontinuity in the a chart.

Then have the chart plot both data series, so the two data series are superimposed


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share