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

Positive and Negative Colors in a Chart

Written by Allen Wyatt (last updated November 17, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


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.

Another approach is to format data points as different colors or shapes, based on whether they are positive or negative. A way to accomplish this is detailed at Jon Peltier's Web site, located here:

http://www.peltiertech.com/Excel/Charts/ConditionalChart1.html

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1999) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Adding Half Spaces to Punctuation

Want a little more space just before some of your punctuation characters? You can add that spacing in a variety of ways, ...

Discover More

Editing Custom Dictionaries

Custom dictionaries can be great, but they take quite a bit of time to create. Word provides a way you can edit your ...

Discover More

Boxes in Boxes

When you insert a text box within another text box, you may expect any text in the outer text box to wrap around the ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Turning the Legend On and Off

When you create a chart in Excel, the program may automatically add a legend that explains the contents of the chart. In ...

Discover More

Adjusting Your View of 3-D Graphs

Do you use Excel's charting capabilities to display three-dimensional views of your data? The program provides a way that ...

Discover More

Changing Chart Type

Excel allows you to add two distinct types of charts to your workbooks: embedded or chart sheets. You can switch between ...

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

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 2 + 2?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

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.