+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    08-17-2009
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2007
    Posts
    3

    Column color based on series name?

    Hi,

    So far I haven't found an answer to this. I have to reformat about 25 charts and I want the columns to be colored based on the series name so that it is the same in all the charts. I found this one macro, and it worked initially when I tested it, but then it just stopped and I can't get it to work again!? Basically, you are supposed to just create a set of color coded index cells.

    Any help appreciated (or why this stupid macros might stop working).

    Code:
    Sub ColorBySeriesName()
      Dim rPatterns As Range
      Dim iSeries As Long
      Dim rSeries As Range
    
      Set rPatterns = ActiveSheet.Range("A1:A4")
      With ActiveChart
        For iSeries = 1 To .SeriesCollection.Count
          Set rSeries = rPatterns.Find(What:=.SeriesCollection(iSeries).Name, _
              LookAt:=xlWhole)
          If Not rSeries Is Nothing Then
            .SeriesCollection(iSeries).Interior.ColorIndex = _
                rSeries.Interior.ColorIndex
          End If
        Next
      End With
    End Sub
    Last edited by mlazenby; 08-18-2009 at 11:29 AM. Reason: code tags

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

    Re: Column color based on series name?

    Please add code tags to your post.

    Post workbook example where the code is not working.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    08-17-2009
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Column color based on series name?

    Ok - after I fixed the code, the macros will run - until you save the sheet. When you reopen the sheet and try to run it gives the following error:

    With ActiveChart.SeriesCollection(1)

    Run Time Error 91
    Object variable or with block variable not set

    WTF?

    I attached the worksheet with the macro.
    Attached Files Attached Files
    Last edited by mlazenby; 08-18-2009 at 01:04 PM. Reason: Wrong - problem still exists

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

    Re: Column color based on series name?

    When you reopen the workbook a cell would be active rather than a chart.
    If you select the chart first then run the code does it work again?

    Try using code that does not require the chart to be selected
    Code:
      Set rPatterns = ActiveSheet.Range("A1:A2")
      With ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1)
        vCategories = .XValues
        For iCategory = 1 To UBound(vCategories)
          Set rCategory = rPatterns.Find(What:=vCategories(iCategory))
          .Points(iCategory).Interior.ColorIndex = rCategory.Interior.ColorIndex
        Next
      End With
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    08-17-2009
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Column color based on series name?

    Jeez! And I could have sworn I checked that. Must have been at the same time I mixed up the two code snippets.

    Thanks.

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