Search code examples
excelvbapivot-chart

Add data and format Pivot Chart using VBA Excel


I am struggling to format a Pivotchart & add data to the chart using VBA. I have setup the code to create the pivot chart which is working fine and I can manually format & add data but whenever I try and mimic the macro recorder or anything I have read online it seems to fail stated 'Application-defined or object defined error.'

I have tried to change the code to change objChart to ActiveChart trying to mimic what I done when using the macro recorder but this still fails. I tried the recorder after my initial attempts failed.

I have also failed to have the ChartTitle.Add work so I can amend the title that is visible (Currently nothing shows as a title).

ActionTracker.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    Actions, Version:=6).CreatePivotTable _
    TableDestination:=CtyDrng, TableName:="Country Overview", _
    DefaultVersion:=6


        Dim pt As PivotTable
        Dim PtRange As Range
        Dim ChrtRange As Range
        Dim objChart As Chart
        Dim chtSeries As SeriesCollection

Set pt = CountryDash.PivotTables("Country Overview")

Set PtRange = pt.TableRange1

With PtRange
    Set ChrtRange = .Offset(, 1).Resize(1, .Columns.Count - 1)
    Set ChrtRange = Union(ChrtRange, .Offset(.Rows.Count - 1, 1).Resize(1, .Columns.Count - 1))
End With

Set objChart = CountryDash.Shapes.AddChart.Chart

With objChart
    .SetSourceData ChrtRange
    .ChartType = xlBarStacked100
    .ShowAllFieldButtons = True
'        .ChartTitle.Add
    '.ChartTitle.Select
'        .ChartTitle.Text = "Country Overview"
End With
    With objChart.PivotLayout.PivotTable.PivotFields("Country")
    .Orientation = xlRowField
    .Position = 1
End With
With objChart.PivotLayout.PivotTable.PivotFields("Region")
    .Orientation = xlRowField
    .Position = 2
End With
With objChart.PivotLayout.PivotTable.PivotFields("Overdue")
    .Orientation = xlRowField
    .Position = 3
End With
   objChart.PivotLayout.PivotTable.AddDataField objChart.PivotLayout. _
    PivotTable.PivotFields("Overdue"), "Overdue", xlCount

I'm expecting the code to create a pivot chart by country then region showing the overdue actions with 3 different categories 'Implemented', 'In Progress' & 'Overdue'.


Solution

  • Pardon if this doesn't fit perfectly, but I haven't attempted to use the adddatafield, which I assume is causing an issue since you're adding to a chart, not the pivot cache.

    You can avoid that by associating with a pivotfield which is already available in the pivotcache (called by header):

    With pivotsheet.PivotTables("CHART_NAME")
        'Insert Row Fields
        With .PivotFields("Country")
            .Orientation = xlRowField
        End With
        'Insert Data Field
        With .PivotFields("Overdue")
            .Orientation = xlDataField
            .Function = xlCount
            .NumberFormat = "#,##0"
            .Name = "Number Overdue"
        End With
    End With
    

    To your point of adding a name to the pivottable, think about how you're generating... don't just append things, action as they occur such that:

    'Define Pivot Cache
    lrs = source.Cells(source.Rows.Count, 1).End(xlUp).Row
    Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="SOURCE_SHEET!A1:B" & lrsource)
    'Insert Blank Pivot Table
    Set pt = pc.CreatePivotTable(TableDestination:=pivotsheet.Cells(1, 1), TableName:="CHART_NAME")
    

    After you've added the pivot table start adding the page fields, the column fields, the rows, and data fields.