+ Reply to Thread
Results 1 to 5 of 5

VBA code to change pivot chart line on 12 charts

Hybrid View

  1. #1
    Registered User
    Join Date
    10-18-2007
    MS-Off Ver
    2010
    Posts
    90

    VBA code to change pivot chart line on 12 charts

    Good morning,
    I have a workbook with 12 pivot charts on 2 sheets. I need to change the lines to all the same type, just different colors. Is there a way I can code this? I currently have the below, but I don't think it would be very efficient to code this for each of the 12 charts. Also note, some of the charts will have only 3 Series while others will have up to 8.

    Sub Macro3()
    '
        ActiveSheet.ChartObjects("Chart 10").Activate
        ActiveChart.SeriesCollection(1).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveChart.SeriesCollection(2).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveChart.SeriesCollection(3).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveChart.SeriesCollection(4).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveChart.SeriesCollection(5).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveChart.SeriesCollection(6).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveChart.SeriesCollection(7).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveChart.SeriesCollection(8).Select
        With Selection.Border
            .Weight = xlThin
            .LineStyle = xlAutomatic
        End With
        With Selection
            .MarkerBackgroundColorIndex = xlAutomatic
            .MarkerForegroundColorIndex = xlAutomatic
            .MarkerStyle = xlTriangle
            .Smooth = False
            .MarkerSize = 5
            .Shadow = False
        End With
        ActiveWindow.Visible = False
        Windows("Digital Marketing POS Sales 041809.xls").Activate
        ActiveCell.Range("A1").Select
    End Sub
    Thank you for your assistance!
    N

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VBA code to change pivot chart line on 12 charts

    This should do all charts on the active sheet

    Sub xx()
    
        Dim lngSeries As Long
        Dim chtTemp As ChartObject
        
        For Each chtTemp In ActiveSheet.ChartObjects
            With chtTemp.Chart
                For lngSeries = 1 To .SeriesCollection.Count
                    With .SeriesCollection(lngSeries)
                        With .Border
                            .Weight = xlThin
                            .LineStyle = xlAutomatic
                        End With
                        .MarkerBackgroundColorIndex = xlAutomatic
                        .MarkerForegroundColorIndex = xlAutomatic
                        .MarkerStyle = xlTriangle
                        .Smooth = False
                        .MarkerSize = 5
                        .Shadow = False
                    End With
                Next
            End With
        Next
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    10-18-2007
    MS-Off Ver
    2010
    Posts
    90

    Re: VBA code to change pivot chart line on 12 charts

    Hey, Andy, thanks so much! That was amazingly simple (at least for me to copy and paste)!

    Now the recipient of this report has taken his question one step further. Each of the series represents a retailer and as stated previously each retailer may or may not be in the resulting pivot chart. On each chart he would like there to be consistency among each of the retailer colors, that is, to see Retailer A always in yellow, Retailer B always in blue, Retailer C always in green, etc. If Retailer A is not represented on a given chart then another retailer should not be yellow. Does that make sense and is this possible to program?

    Thanks again!
    N

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VBA code to change pivot chart line on 12 charts

    Something along these lines should do it.
    http://peltiertech.com/WordPress/vba...ategory-label/

  5. #5
    Registered User
    Join Date
    10-18-2007
    MS-Off Ver
    2010
    Posts
    90

    Thumbs up Re: VBA code to change pivot chart line on 12 charts

    Thanks, Andy, that site did it for me!

    Final code:
    Sub ColorBySeriesName()
      Dim rPatterns As Range
      Dim iSeries As Long
      Dim rSeries As Range
      Dim iColorIndex As Long
      Dim chtTemp As ChartObject
     
      Set rPatterns = ActiveSheet.Range("BC1:BD8")
    
      For Each chtTemp In ActiveSheet.ChartObjects
    
      With chtTemp.Chart
        For iSeries = 1 To .SeriesCollection.Count
          Set rSeries = rPatterns.Find(What:=.SeriesCollection(iSeries).Name)
          If Not rSeries Is Nothing Then
            iColorIndex = rSeries.Interior.ColorIndex
            With .SeriesCollection(iSeries)
              .Border.ColorIndex = iColorIndex
              .MarkerForegroundColorIndex = iColorIndex
              .MarkerBackgroundColorIndex = iColorIndex
              .MarkerStyle = xlTriangle
            End With
          End If
        Next
      End With
      Next
    End Sub
    Thank you so much for your help!!
    N

+ 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