+ Reply to Thread
Results 1 to 8 of 8

Matching graph colours with legend error

  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:
    Please Login or Register  to view this content.
    because there is no graph 4.

    Now adding to the beginning of the code an error handler
    Please Login or Register  to view this content.
    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:

    Please Login or Register  to view this content.

    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

    Please Login or Register  to view this content.

    and not

    Please Login or Register  to view this content.

    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':

    Please Login or Register  to view this content.
    Last edited by dunda1985; 07-08-2011 at 10:36 AM.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Matching graph colours with legend error

    Instead of coding the coloring separately, loop through each series:

    Please Login or Register  to view this content.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

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

    Re: Matching graph colours with legend error

    Hi Davegugg,

    I've already got the code working in a looped function. I broke it out to describe the problem. Thanks for your help, however it's still not resolving my problem. The problem is the last avaialble graph is still selected, and the next part of the code that runs without the error will continue to run with the next part of the code:

    Please Login or Register  to view this content.
    In other words, say I only have 6 graphs out of a capacity for 30 graphs (30 preassigned colours). The 6th graph is still selected with the code:
    Please Login or Register  to view this content.
    and due to handling error,
    Please Login or Register  to view this content.
    will still be selected all the way to
    Please Login or Register  to view this content.
    The problem then becomes that the last entry in the legend is not the same colour as its corresponding graph.

    Many thanks in advance for your help.

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Matching graph colours with legend error

    I don't understand. You say you have a variable number of graphs, but your code above all only works on one graph, ChartA. Are you talking about multiple charts, or multiple data series? Perhaps you could post a small sample workbook?

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,331

    Re: Matching graph colours with legend error

    Don't select:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Matching graph colours with legend error

    D'oh! Thank you for the simply obvious solution romperstomper!

    Sorry davegugg, each data series on a chart is technically a 'graph'. Sorry for the confusion and many thanks for your help.

    Also, how do I mark this thread as solved please?
    Last edited by dunda1985; 07-08-2011 at 10:35 AM.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,331

    Re: Matching graph colours with legend error

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

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

    Re: Matching graph colours with legend error

    done, thanks

+ Reply to Thread

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