Results 1 to 2 of 2

Thread: How to Insert new PowerPivotChart into Excel 2013 via VBA?

  1. #1
    Seeker Ted Murphy's Avatar
    Join Date
    Apr 2016

    Question How to Insert new PowerPivotChart into Excel 2013 via VBA?

    Register for a FREE account, and/
    or Log in to avoid these ads!

    It is not possible to insert a PowerPivotChart into a worksheet in Excel 2013 without opening the Data Model window, which can be intimidating for an end user consumer.

    The only alternative is to create a VBA macro that will perform the task.

    Matt Allington, with assistance from Dominik Petri, published the script below to create a insert a PowerPivotTable. It work a treat.

    I have tried to create a variation that would allow a PowerPivotChart to be inserted. However, my VBA skills don't stretch that far and I came unstuck.

    Does anyone have the time (and capability) to modify the Macro so that it will work for a Chart?

    Any help would be much appreciated. The macro will be very useful to anyone using PowerPivot in Excel 2013.

    Macro for Pivot Table as published by Matt Allington (with input from Dominik Petri)

    Sub InsertPowerPivotTable()Dim PowerPivotCache As Excel.PivotCache
    Dim NewPowerPivotTable As Excel.PivotTable 
    ‘ Create new cache 
    Set PowerPivotCache = ActiveWorkbook.PivotCaches.Create( _ 
     SourceType:=xlExternal, _ 
     SourceData:=ActiveWorkbook.Connections(“ThisWorkbookDataModel”), _
    ‘ Create PivotTable 
    Set NewPowerPivotTable = PowerPivotCache.CreatePivotTable( _ 
     TableDestination:=ActiveCell, _ 
    ‘ Settings for new PowerPivotTable 
    With NewPowerPivotTable 
    .RowAxisLayout xlTabularRow 
    .HasAutoFormat = False 
    End With 
    ‘ Cleanup 
    Set NewPowerPivotTable = Nothing 
    Set PowerPivotCache = Nothing
    End Sub
    Last edited by Ken Puls; 2016-04-15 at 04:40 PM. Reason: Added code tags

  2. #2
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Nanaimo, BC, Canada
    Blog Entries
    Excel Version
    Excel Office 365 Insider
    Hey Ted,

    I'm not in love with this macro, as it seems to require creating a chart sheet, then turning it into a proper chart. At the end of the day, though, it does work:

    Sub CreatePivotChart()
        Dim objPivot As PivotTable
        Dim objChart As Chart
        Dim sPivotName As String
        Dim sPivotSheet As String
        'Set the name of the sheet that holds the pivot table and the name of the pivot chart
        sPivotSheet = "Dashboard"
        sPivotName = "PivotTable2"
        'Bind to the desired pivot table.
        On Error Resume Next
        Set objPivot = Worksheets(sPivotSheet).PivotTables(sPivotName)
        If Err.Number <> 0 Then Exit Sub
        On Error GoTo 0
        'This creates a new chart sheet (don't worry, we'll change that later.)
        Set objChart = Charts.Add
        'Configure the PivotChart here.
        With objChart
            .SetSourceData objPivot.TableRange1
            .ChartType = xlLine
        End With
        'Convert from chart sheet into chart
        ActiveChart.Location Where:=xlLocationAsObject, Name:=objPivot.Parent.Name
    End Sub
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: -||- Blog: -||- Forums:
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts