Consulting

Results 1 to 7 of 7

Thread: Create a simple chart in a sheet where a pivot table already exists

  1. #1

    Create a simple chart in a sheet where a pivot table already exists

    Hello,

    So, here is the situation: I have a Sheet on which I created a pivot table based on some data from another sheet as well as a normal table (based on the pivot table, using the GETPIVOTDATA function). I would like to create a simple chart based on the normal table using VBA. Now, the following procedure automatically creates a pivot chart based on the pivot table in my sheet:
    Dim myChart As ChartObject
    Set myChart = Worksheets(OVERVIEW).ChartObjects.Add(Left:=100, Width:=375, Top:=100, Height:=225)
    With myChart.Chart
        .ChartType = xlColumnStacked
        .SetSourceData Source:=Range("Overview!$B$26:$E$28") <---------------------- ERROR HERE
    End With
    And what I really would like to do is prevent Excel from automatically creating a pivot chart based on the pivot table present in my sheet, or allow me to redefine the type of the chart and the source data as I wish. Right now, when the line ".SetSourceData Source:=Range("Overview!$B$26:$E$28")" is executed, Excel returns the error "Run-time error '-2147467259 (80004005)': Method 'SetSourceData' of object '_Chart' failed".

    I've been looking around but have not found any explanation I could understand regarding this error. I assume this simply tells me that I'm not allowed to re-allocate the source data, but that is pure speculation.

    Any help would be much appreciated!

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi. try this:
    Worksheets("Overview").Range("$B$26:$E$28")
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Hi,
    Unfortunately, this returns the same error. And again, it seems that the chart created is automatically a pivot chart using the same filters, rows, columns, and values as the pivot table already present in the sheet.

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    did you try recording a macro while creating the PT?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    In fact, I do create the pivot table using VBA as well.
    I did try to create the simple chart in question by recording a macro. However, when copying the code onto my module, it does not execute (same error as shown earlier). As a matter of fact, the macro does not even execute if I simply delete the chart and run the recorded macro.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    can you post a workbook with sample data?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    Well, I did find a solution but it is a bit dirty. Instead of using a ChartObject, I use the "ActiveChart" object to assign the range of my chart. This and rebooting my CPU seems to have done the trick... Not very satisfactory though.

Posting Permissions

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