Pivot Chart - change with VBA

Apfel007

New Member
Joined
Nov 1, 2013
Messages
9
Hi there,
I'm quite new on vba and I try to understand how to change a chart with VBA.

I found this usefull link Dynamic Chart using Pivot Table and VBA - Peltier Tech Blog.

But I don't understand it completely.

Let's have a look on the screenshot:
https://www.dropbox.com/s/dgvl08mggx7ke76/pivot_chart_dynamic.png

In the end I like to recreate a chart by choosing the year number in a combobox. But first I like to understand how to recreate a chart.
Each category should get it's own chart, which compare two years, but I will start with one chart.

What is the starting point to say " use year 2012 + 2013 and category A " and draw the chart line?

Code:
Sub UpdateChartFromPivot()
  'update charts on cheet
    Dim rCategories As Range  '??
    Dim rValues As Range ' ??
    
    Dim rYear As Range
    Dim rMonth As Range
    
    Dim pt As PivotTable
    Dim cht As Chart
    Dim iMonth As Long
    
    Dim nMonth As Long 'number of month


    ' Define the Pivot Table   - OK
    'Set pt = ActiveSheet.PivotTables(1)
    Set pt = ActiveSheet.PivotTables("PivotTable2")


    ' Define the Ranges with set!   What RANGE is it?
    Set rValues = pt.DataBodyRange 'Werte felder
        
    'Y in pivot table?
    With pt.RowRange
        Set rCategories = .Offset(2).Resize(.Rows.Count - 2)   '??
    End With
    
    ' Monate !! x achse
    Set rMonth = pt.ColumnRange.Rows(2)


    ' Define the Chart
    'Set cht = ActiveSheet.ChartObjects(1).Chart
    Set cht = ActiveSheet.ChartObjects("Diagramm 1").Chart


    ' How Many Month/Series?
    nMonth = rMonth.Columns.Count ' Anzahl der Monate ->Columns count
    
   '******  this I completely not understand!!!  ************
    ' Remove or Add Series until Chart Has Correct Number
    Select Case cht.SeriesCollection.Count - nMonth
        Case Is > 0
            ' too many: remove excess series
            For iMonth = cht.SeriesCollection.Count To nMonth + 1 Step -1
                cht.SeriesCollection(iMonth).Delete
            Next
        Case Is < 0
            ' too few: add sufficient series
            For iMonth = cht.SeriesCollection.Count + 1 To nMonth
                cht.SeriesCollection.NewSeries
            Next
        Case Else
            ' just right
    End Select


    ' Populate Each Series
    For iMonth = 1 To nMonth
        With cht.SeriesCollection(iMonth)
            '.Name = rMonth.Columns(iMonth ) ' Monate
            '.Values = rValues.Columns(iMonth )
            '.'XValues =  ' Jahreszahlen
            .Name = rMonth.Columns(iMonth)
            .Values = rValues.Columns(iMonth)
            .XValues = rMonth.Columns(iMonth) 'rCategories
            .Border.LineStyle = xlContinuous
        End With
    Next
    
    End Sub

Thanks for your help

Cheers K.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top