Format Chart Based on Point Values

A recent post to the Microsoft Excel Charting Discussion Group asks . . . how can I change the pattern of a column based on that column's value. Assuming an embedded chart, I responded with the following procedure:

Sub ChangePatterns()

    Application.ScreenUpdating = False

    Dim Cht As Chart
    Dim Srs As Series
    Dim Pts As Points

    Set Cht = ActiveChart
    Set Srs = Cht.SeriesCollection(1)
    Set Pts = Srs.Points

    Cnt = 1

    For Each Pt In Srs.Values

        Srs.Points(Cnt).Select

        'Sales greater than 10000
        If Pt > 10000 Then

            With Selection
                .Fill.Visible = True
                .Fill.Patterned Pattern:=msoPatternWideUpwardDiagonal
                .Fill.ForeColor.SchemeColor = 42
                .Fill.BackColor.SchemeColor = 34
            End With

        'Sales less than or equal to 10000
        ElseIf Pt <= 10000 Then

            With Selection
                .Fill.Visible = True
                .Fill.Patterned Pattern:=msoPatternLightHorizontal
                .Fill.ForeColor.SchemeColor = 43
                .Fill.BackColor.SchemeColor = 22
            End With

        End If

        Cnt = Cnt + 1

    Next Pt

    ActiveChart.Deselect

End Sub

-- Just thought I would document it for my own reference. Is there a better way to write this?


Posted on June 26, 2007 | Filed under: Charts | Comments (2) | Permalink
Page 1 of 1 pages

Comments

Not sure it’s better but it is differently, slightly ;)

The values to use are stored in variables and then applied.
No selection of point is required.
Added checking for empty and N/A cells.

Sub ChangePatterns()

    
Dim Cht As Chart
    Dim Srs
As Series
    Dim Pt
As Variant
    Dim Cnt
As Long
    Dim lngPattern
As MsoPatternType
    Dim lngForeScheme
As Long
    Dim lngBackScheme
As Long
    
    Application
.ScreenUpdating = False
    
    Set Cht
= ActiveChart
    Set Srs
= Cht.SeriesCollection(1)

    
Cnt = 1

    
For Each Pt In Srs.Values
    
        
' handle empty or N/A cells
        If Not IsError(Pt) Then
            
            If Pt > 10000 Then
                
            '
Sales greater than 10000
                lngPattern
= msoPatternWideUpwardDiagonal
                lngForeScheme
= 42
                lngBackScheme
= 34
    
            
Else
            
            
'Sales less than or equal to 10000
                lngPattern = msoPatternLightHorizontal
                lngForeScheme = 43
                lngBackScheme = 22
            End If
            
            '
apply values
            With Srs
.Points(Cnt).Fill
                
.Visible = True
                
.Patterned lngPattern
                
.ForeColor.SchemeColor = lngForeScheme
                
.BackColor.SchemeColor = lngBackScheme
            End With
        
        End
If
        
        
Cnt = Cnt + 1

    Next Pt

    ActiveChart
.Deselect

End Sub

 

Posted by Andy Pope  on  06/28  at  05:46 AM

Thanks Andy.  Yours is a much better way to go - especially by not needing to select the point.  Adding the error check for empty and N/A cells is something I would have never thought of.  I can see the need to add that option to any charting code now.  Thanks again.

Posted by .(JavaScript must be enabled to view this email address)  on  07/01  at  05:22 AM
Page 1 of 1 pages

Comment Entry

Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?

Statistics

  • Total Entries - 136
  • Current Viewers - 26

Categories

Entries by Day

Jul - 2010
S M T W T F S
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Recent Comments

Syndicate