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?
Thanks for your help
Cheers K.
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.