For a couple of days now I'm trying to deblock an issue that I have when creating a macro to create three embedded charts from three existing pivottables.
I seem to can't get around the issue, so I direct myself to this forum in the hope to get an answer to why my code constantly get stuck.
Underneath you can find my code.
Before that I run this macro, I already created several pivottables through a separate macro. That macro works without problems, but when I subsequently run the following macro (to create my pivotcharts) I get an error:
Sub WeeklyBEApplCharts1()
'Remove all existing chartsobjects in the worksheet
If Worksheets("Fixed").ChartObjects.Count > 0 Then Worksheets("Fixed").ChartObjects.Delete
'Make pivotchart 1 based on pivottable Rep vs Age
Dim oChObj1 As ChartObject
Set oChObj1 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj1.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=Sheets("Fixed").PivotTables("RepVsAge").TableRange1
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
.SeriesCollection(2).Format.Fill.ForeColor.ObjectThemeColor = msoThemeColorText1
.Axes(xlCategory).ReversePlotOrder = True
.Axes(xlValue).MinimumScale = 0
End With
'Make pivotchart 2 based on pivottable Plan Vs Besl
Dim oChObj2 As ChartObject
Set oChObj2 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj2.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=Sheets("Fixed").PivotTables("PlanVSBesl").TableRange1
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(146, 208, 80)
.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 192, 0)
.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Axes(xlCategory).ReversePlotOrder = True
End With
'Make pivotchart 3 based on pivottable Term Vs Besl
Dim oChObj3 As ChartObject
Set oChObj3 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj3.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=Sheets("Fixed").PivotTables("TermVSBesl").TableRange1 'TableRange1 neemt automatisch de range over van de benoemde pivottabel
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(146, 208, 80)
.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 192, 0)
.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Axes(xlCategory).ReversePlotOrder = True
End With
End Sub
When I run this macro, I constantly get the following error which is located at the setting of the source data for my second pivotchart (so he always succeeds in creating the first chart, but fails at the second one):
Run-time error '-2147467259 (80004005)': Method 'SetSourceData' of object '_Chart' failed
I already found some posts hinting to the solution for this issue of deleting the existing SeriesCollections after creating the chart but before setting the sourcedata.
For this reason, I added for all three pivottables a piece of code deleting the existing Seriescollections.
Example of the additional code for the first pivotchart (and which I repeat also for the other two):
Dim oChObj1 As ChartObject
Set oChObj1 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj1.Chart
.ChartType = xlBarStacked
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
.SetSourceData Source:=Sheets("Fixed").PivotTables("RepVsAge").TableRange1 'TableRange1 neemt automatisch de range over van de benoemde pivottabel
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
.SeriesCollection(2).Format.Fill.ForeColor.ObjectThemeColor = msoThemeColorText1
.Axes(xlCategory).ReversePlotOrder = True
.Axes(xlValue).MinimumScale = 0
End With
When I do this, I don't get the preceding error anymore, but I get another one aiming at the .SeriesCollection(1).Delete line:
Run-time error '1004': Application-defined or object-defined error
Ironically when I go to the immediate window afterwards and count the number of existing SeriesCollections (through ?oChObj1.Chart.SeriesCollection.Count), I do get as a result that 2 seriescollections exist. S
o I don't understand why he errors and doesn't want to delete the seriescollections.
And because I got the error, I tried deleting this piece of code again, but this results of course in getting the previous error (on setsourcedata).
So it looks like I'm in a Catch 22 situation!
Any help is very much appreciated to help me out!
I seem to can't get around the issue, so I direct myself to this forum in the hope to get an answer to why my code constantly get stuck.
Underneath you can find my code.
Before that I run this macro, I already created several pivottables through a separate macro. That macro works without problems, but when I subsequently run the following macro (to create my pivotcharts) I get an error:
Sub WeeklyBEApplCharts1()
'Remove all existing chartsobjects in the worksheet
If Worksheets("Fixed").ChartObjects.Count > 0 Then Worksheets("Fixed").ChartObjects.Delete
'Make pivotchart 1 based on pivottable Rep vs Age
Dim oChObj1 As ChartObject
Set oChObj1 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj1.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=Sheets("Fixed").PivotTables("RepVsAge").TableRange1
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
.SeriesCollection(2).Format.Fill.ForeColor.ObjectThemeColor = msoThemeColorText1
.Axes(xlCategory).ReversePlotOrder = True
.Axes(xlValue).MinimumScale = 0
End With
'Make pivotchart 2 based on pivottable Plan Vs Besl
Dim oChObj2 As ChartObject
Set oChObj2 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj2.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=Sheets("Fixed").PivotTables("PlanVSBesl").TableRange1
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(146, 208, 80)
.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 192, 0)
.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Axes(xlCategory).ReversePlotOrder = True
End With
'Make pivotchart 3 based on pivottable Term Vs Besl
Dim oChObj3 As ChartObject
Set oChObj3 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj3.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=Sheets("Fixed").PivotTables("TermVSBesl").TableRange1 'TableRange1 neemt automatisch de range over van de benoemde pivottabel
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(146, 208, 80)
.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 192, 0)
.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Axes(xlCategory).ReversePlotOrder = True
End With
End Sub
When I run this macro, I constantly get the following error which is located at the setting of the source data for my second pivotchart (so he always succeeds in creating the first chart, but fails at the second one):
Run-time error '-2147467259 (80004005)': Method 'SetSourceData' of object '_Chart' failed
I already found some posts hinting to the solution for this issue of deleting the existing SeriesCollections after creating the chart but before setting the sourcedata.
For this reason, I added for all three pivottables a piece of code deleting the existing Seriescollections.
Example of the additional code for the first pivotchart (and which I repeat also for the other two):
Dim oChObj1 As ChartObject
Set oChObj1 = Sheets("Fixed").ChartObjects.Add(100, 100, 100, 100)
With oChObj1.Chart
.ChartType = xlBarStacked
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
.SetSourceData Source:=Sheets("Fixed").PivotTables("RepVsAge").TableRange1 'TableRange1 neemt automatisch de range over van de benoemde pivottabel
.ShowAllFieldButtons = False
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
.SeriesCollection(2).Format.Fill.ForeColor.ObjectThemeColor = msoThemeColorText1
.Axes(xlCategory).ReversePlotOrder = True
.Axes(xlValue).MinimumScale = 0
End With
When I do this, I don't get the preceding error anymore, but I get another one aiming at the .SeriesCollection(1).Delete line:
Run-time error '1004': Application-defined or object-defined error
Ironically when I go to the immediate window afterwards and count the number of existing SeriesCollections (through ?oChObj1.Chart.SeriesCollection.Count), I do get as a result that 2 seriescollections exist. S
o I don't understand why he errors and doesn't want to delete the seriescollections.
And because I got the error, I tried deleting this piece of code again, but this results of course in getting the previous error (on setsourcedata).
So it looks like I'm in a Catch 22 situation!
Any help is very much appreciated to help me out!