+ Reply to Thread
Results 1 to 6 of 6

Change colour of chart series

Hybrid View

  1. #1
    Registered User
    Join Date
    01-04-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    16

    Change colour of chart series

    hey

    I'm attempting to write a code that will change the colour of a series in a chart based on the name of each series.

    i.e. I have Roads, Water and Rail as three of my series names. can i somehow assign the colours yellow, blue and red to these series using macro code.

    Thanks Heaps
    Last edited by downunderthunder; 01-06-2010 at 09:26 PM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Change colour of chart series

    Perhaps this can be a start you can adapt to your workbook. This assumes a chart object in the worksheet as opposed to a Chart Sheet.
    Option Explicit
    
    Sub ChangeSeriesColor()
    
    Dim co As ChartObject, x, y, z
    
        x = Range("I1").Value
        y = Range("I2").Value
        z = Range("I3").Value
        
        On Error Resume Next
        
        For Each co In ActiveSheet.ChartObjects
            co.Chart.SeriesCollection(1).Interior.ColorIndex = x
            co.Chart.SeriesCollection(2).Interior.ColorIndex = y
            co.Chart.SeriesCollection(3).Interior.ColorIndex = z
        Next co
        
    End Sub
    You could run the code by using the worksheet change event to call it. Thus, by entering numeric values in cells, automatically change the color. See cells I1:I3 in the attached.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Target, Range("I1:I3")) Is Nothing Then
            Run "ChangeSeriesColor"
        End If
    
    End Sub
    Attached Files Attached Files
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    01-04-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Change colour of chart series

    Hi

    This is not quite what I'm looking for.

    I have applied the following code to change the colours on pie charts:

         ' Iterates through all pie charts in the dashboard and apply colours to the appropriate legends
        Dim savePtLabel As String
        Dim ThisPt As String
         
        Dim NumPoints As Integer
        Dim x As Integer
         
        Dim pie As ChartObject
        Sheets("Revenue By Sector").Select
        For Each pie In ActiveSheet.ChartObjects
             
             ' Check that the current chart object is a pie chart
            If pie.Chart.ChartType = xlPie Then
                 
                NumPoints = pie.Chart.SeriesCollection(1).Points.Count
                For x = 1 To NumPoints
                     
                     '  Save the label currently  attached to the current slice
                    If pie.Chart.SeriesCollection(1).Points(x).HasDataLabel = True Then
                        savePtLabel = pie.Chart.SeriesCollection(1).Points(x).DataLabel.Text
                    Else
                        savePtLabel = ""
                    End If
                     
                     '  Assign a new data label of just the point  name
                    pie.Chart.SeriesCollection(1).Points(x).ApplyDataLabels Type:= _
                    xlDataLabelsShowLabel, AutoText:=True
                    ThisPt = pie.Chart.SeriesCollection(1).Points(x).DataLabel.Text
                     
                     ' Based on the label of this slice, set the color
                    Select Case ThisPt
                    Case "Water"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 41
                    Case "Roads"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 19
                    Case "Rail"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 22
                    Case "Ports"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 25
                    Case "Industrial"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 30
                    Case "Resource Engineering"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 35
                    Case "Airports"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 34
                    Case "Energy"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 38
                    Case "Oil & Gas"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 38
                    Case "Water Treatment"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 41
                    Case "Water Transmission"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 41
                    Case "Wastewater Treatment"
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 41
                    Case Else
                         ' Aroo! The label of the current slice doesn't match any expected labels
                        pie.Chart.SeriesCollection(1).Points(x).Interior.ColorIndex = 1
                    End Select
                     
                     ' Return the label to it's original state
                    pie.Chart.SeriesCollection(1).Points(x).ApplyDataLabels Type:=xlDataLabelsShowValue, AutoText:=True
                     
                Next x
            End If
        Next

    I would like to apply somethin similar to a 'Stacked Area' Chart

    Cheers

    DUT

    p.s. sorry I don't know how to add specific windows in my message for the code
    Last edited by downunderthunder; 01-06-2010 at 01:19 AM.

  4. #4
    Registered User
    Join Date
    01-04-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Change colour of chart series

    Hey

    Sorry I'm new to this forum. I think its fixed now. Could someone please help me with this problem

    Cheers

    DUT

  5. #5
    Registered User
    Join Date
    01-04-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Change colour of chart series

    Bump no response

  6. #6
    Registered User
    Join Date
    01-04-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Change colour of chart series

    Solved Myself

    Sheets("Human Resource Requirements").Select
    For x = 1 To ActiveChart.SeriesCollection.Count
    Select Case ActiveChart.SeriesCollection(x).Name
            Case "Water"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 41
            Case "Roads"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 19
            Case "Rail"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 22
            Case "Ports"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 25
            Case "Industrial"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 30
            Case "Resource Engineering"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 35
            Case "Airports"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 34
            Case "Energy"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 38
            Case "Oil and Gas"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 38
            Case "Water Treatment"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 41
            Case "Water Transmission"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 41
            Case "Wastewater Treatment"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 41
            Case "Current"
                        ActiveChart.SeriesCollection(x).Interior.ColorIndex = 2
                        ActiveChart.SeriesCollection(x).Border.ColorIndex = 3
                        ActiveChart.SeriesCollection(x).Border.Weight = xlThick
                    End Select
                Next x

+ 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.6.0 RC 1