Results 1 to 8 of 8

Matching graph colours with legend error

Threaded View

  1. #1
    Registered User
    Join Date
    06-16-2011
    Location
    dublin, ireland
    MS-Off Ver
    Excel 2003
    Posts
    8

    Matching graph colours with legend error

    Hi guys,

    I've been having problems with trying to set certain colours to certain graphs within an excel chart and then matching that colour to the font of a legend. The run-time error 1004 appears when there is no graph to select.

    See the code below for reference. So for example, say I have built this sheet to automate for 5 graphs (capacity) within the one chart. Say I only have 3 actual graphs, therefore the error will occur on the line of code:
    ActiveChart.SeriesCollection(4).Select
    because there is no graph 4.

    Now adding to the beginning of the code an error handler
     on error resume next
    deals with this problem, however while it skips the error in the above code, it does not skip the body of code next to it. In other words, because prior to the error, graph 3 was selected, it will continue with the 'workable' codes:

        With Selection.Border
            .ColorIndex = 7     'MAGENTA
        End With
    Range("legenda").Offset(4, 0).Select
    Selection.Font.ColorIndex = 7
    
        With Selection.Border
            .ColorIndex = 15     'GREY
        End With
    Range("legenda").Offset(5, 0).Select
    Selection.Font.ColorIndex = 15

    Thus working its way all the way to the last line of code. So in this particular example with 3 available graphs out of a capacity of 5, the colour index of the third (and last available) graph will be

         With Selection.Border
            .ColorIndex = 15     'GREY

    and not

        With Selection.Border
            .ColorIndex = 5     'BLUE

    I hope I'm making sense. I would really appreciate it if anyone could come up with a solution or workaround please? Many many thanks in advance.

    See below for the 'full code':

    Sub private()
    
                        'GRAPH1
    On Error Resume Next
    ActiveSheet.ChartObjects("ChartA").Activate
    ActiveChart.SeriesCollection(1).Select
        With Selection.Border
            .ColorIndex = 3     'RED
        End With
    Range("legenda").Offset(1, 0).Select
    Selection.Font.ColorIndex = 3
    
    
    
                        'GRAPH2
    
    ActiveSheet.ChartObjects("ChartA").Activate
    ActiveChart.SeriesCollection(2).Select
        With Selection.Border
            .ColorIndex = 4     'GREEN
        End With
    Range("legenda").Offset(2, 0).Select
    Selection.Font.ColorIndex = 4
    
                        
                        
                        'GRAPH3
    
    ActiveSheet.ChartObjects("ChartA").Activate
    ActiveChart.SeriesCollection(3).Select
        With Selection.Border
            .ColorIndex = 5     'BLUE
        End With
    Range("legenda").Offset(3, 0).Select
    Selection.Font.ColorIndex = 5
    
    
    
                        'GRAPH4
    
    ActiveSheet.ChartObjects("ChartA").Activate
    ActiveChart.SeriesCollection(4).Select
        With Selection.Border
            .ColorIndex = 7     'MAGENTA
        End With
    Range("legenda").Offset(4, 0).Select
    Selection.Font.ColorIndex = 7
    
    
    
                        'GRAPH5
    
    ActiveSheet.ChartObjects("ChartA").Activate
    ActiveChart.SeriesCollection(5).Select
        With Selection.Border
            .ColorIndex = 15     'GREY
        End With
    Range("legenda").Offset(5, 0).Select
    Selection.Font.ColorIndex = 15
    Last edited by dunda1985; 07-08-2011 at 10:36 AM.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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