+ Reply to Thread
Results 1 to 11 of 11

Thread: Conditional Chart Formatting

  1. #1
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Conditional Chart Formatting

    Hello,

    I need help in conditionally formatting a chart.
    I the attached file I need to color Item2's Series red if value is below 40 and green if above

    How this can be done?

    Thanks for the help
    Attached Files Attached Files
    Last edited by Bob@Sun; 04-08-2010 at 06:18 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Conditional Chart Formatting

    Being a pivot chart you can not use the usual approach of extra series.

    Instead you need event code. right click the sheet tab for the chart and pick view code. Paste the following.

    Private Sub Chart_Calculate()
        Dim objSeries As Series
        Dim lngPoint As Long
        
        Set objSeries = ActiveChart.SeriesCollection(2)
        For lngPoint = 1 To objSeries.Points.Count
            If Application.WorksheetFunction.Index(objSeries.Values, 1, lngPoint) > 40 Then
                objSeries.Points(lngPoint).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
            Else
                objSeries.Points(lngPoint).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
            End If
        Next
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Conditional Chart Formatting

    Hi Andy,

    The code you provided gives me an error in this part:

    objSeries.Points(lngPoint).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
    Error: Run-Time Error '438'. Object doesn't support this property or method

    Any ideas?

    Thanks for the help!

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Conditional Chart Formatting

    Sorry, that was xl2007 code.

    Private Sub Chart_Calculate()
        Dim objSeries As Series
        Dim lngPoint As Long
        
        Set objSeries = ActiveChart.SeriesCollection(2)
        For lngPoint = 1 To objSeries.Points.Count
            If Application.WorksheetFunction.Index(objSeries.Values, 1, lngPoint) > 40 Then
                objSeries.Points(lngPoint).Interior.ColorIndex = 14
            Else
                objSeries.Points(lngPoint).Interior.ColorIndex = 3
            End If
        Next
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  5. #5
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Conditional Chart Formatting

    Hi Andy,

    The code is great!

    I was wondering how can I change the code to refere if to a cell in another sheet?

    Private Sub Chart_Calculate()
        Dim objSeries As Series
        Dim lngPoint As Long
        
        Set objSeries = ActiveChart.SeriesCollection(1)
        For lngPoint = 1 To objSeries.Points.Count
            'If Application.WorksheetFunction.Index(objSeries.Values, 1, lngPoint) > 2 Then
             If Sheets("Calculations").Range(AI4).Value < 1 Then
                objSeries.Points(lngPoint).Interior.ColorIndex = 14
            Else
                objSeries.Points(lngPoint).Interior.ColorIndex = 3
            End If
        Next
    
    End Sub


    Can you help me with this Please?

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Conditional Chart Formatting

    You need to provide more information as currently all points in the series will be tested against a single cell.

    You need quotes around the cell address.

             If Sheets("Calculations").Range("AI4").Value < 1 Then
    Cheers
    Andy
    www.andypope.info

  7. #7
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Conditional Chart Formatting

    Hi Andy,

    So here is the scenario:

    With the layout in the sample chart, I need to color the column for Item 7 based on the value in Column K in sheet1:
    If Value is 1 - red , if 2 - green

    Can you do that?
    Attached Files Attached Files

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Conditional Chart Formatting

    Assuming each record is discrete then this should work.

    Private Sub Chart_Calculate()
    
        Dim lngIndex As Long
        Dim strOuterLabel As String
        Dim strInnerLabel As String
        Dim strLabel As String
        Dim strTemp As String
        Dim lngRow As Long
            
        On Error Resume Next
        
        For lngIndex = 1 To ActiveChart.SeriesCollection(1).Points.Count
            strLabel = Application.WorksheetFunction.Index(ActiveChart.SeriesCollection(1).XValues, 1, lngIndex)
            strInnerLabel = Split(strLabel, Chr(10))(0)
            strTemp = Trim(Split(strLabel, Chr(10))(1))
            If Len(strTemp) > 0 Then
                strOuterLabel = strTemp
            End If
            
            If strInnerLabel = "Item 7" Then
                Debug.Print "Check Value ", strInnerLabel, strOuterLabel
                lngRow = Application.WorksheetFunction.Match(strOuterLabel, Sheet1.Range("A1:A12"), 0)
                If Sheet1.Cells(lngRow, 11) = 1 Then
                    ActiveChart.SeriesCollection(1).Points(lngIndex).Interior.ColorIndex = 3
                Else
                    ActiveChart.SeriesCollection(1).Points(lngIndex).Interior.ColorIndex = 14
                End If
            End If
        Next
        
    End Sub
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  9. #9
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Conditional Chart Formatting

    Many thanks, it is perfect!

    I still have some questions that are related to the same Pivot Table. Tell me if I need to start a new Post or keeping the discussion here is Ok.

    So, with the same file and layout, is it possible to remove from the chart Items which value is 0?

    In the sample file, I have Values in F2, D3 and H3 = 0 and I need in the chart those Items to be removed. Item 4 for Name 1, Item 2 for Name 2 and Item 6 for Name 2 should not be on the chart.

    I appreciate your help Andy!

    Bob
    Attached Files Attached Files

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Conditional Chart Formatting

    I don't think you can remove those zero items.

    It's all or nothing. If you try to hide Item4, for example, all Item4 details will go.

    If you restructure your data and don't include the zero records then it will do it automatically.
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  11. #11
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Conditional Chart Formatting

    I can't restructure the data, thanks for the help anyway.

    Everything else is perfect!

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0