+ Reply to Thread
Results 1 to 6 of 6

VBA to change data source of pivot table to another pivot table

  1. #1
    Registered User
    Join Date
    11-15-2007
    Posts
    9

    VBA to change data source of pivot table to another pivot table

    I'm building an Excel 2007 workbook that contains multiple pivot tables which are all based on same pivot cache. The data source is external and retrieved by a SQL query from an Access 2007 DB. The SQL query may be changed for a purpose so a new pivot cache is created when it's modified.

    I can use Pivot Table Wizard to manually steer all pivot tables to new master copy of pivot table without a problem. But I want to use VBA code to do the process to same time as I have more then 10 pvt in the workbook. So I use the Excel's macro to record a manual change. Below is what is the VBA code produced by Macro:
    Please Login or Register  to view this content.
    pvtMstrCopy is the name I give to the master pivot table. Cell E77 is inside the target pivot table which I want to change data source.

    But the problem is I can't even re-run this recorded macro. Excel report error message
    Run-time error '1004' PivotTableWizard method of Worksheet class failed
    I add a sheet name to SourceData parameter to "Sheet1!pvtMstrCopy" and it still has the same error.

    It's frustrating since I've been searching same error in various Excel forums for 2 days and I didn't find same one.

    Can anyone tell me how to use VBA to change pivot table data source to another one? Thanks in advance.
    Last edited by huyeote1; 06-13-2011 at 05:38 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: VBA to change data source of pivot table to another pivot table

    Hi huyeole1,

    I recorded two macros that changed the data source for a single pivot table. See if the attached example helps you figure out how to solve your problem. I was unable to make the SourceData equal to a named range.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,016

    Re: VBA to change data source of pivot table to another pivot table

    Something like:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    11-15-2007
    Posts
    9

    Re: VBA to change data source of pivot table to another pivot table

    Hi Marvin,

    Thanks for your help. But MS's VBA reference said ChangePivotCache method desn't work on cache that is connected to an external source.

  5. #5
    Registered User
    Join Date
    11-15-2007
    Posts
    9

    Re: VBA to change data source of pivot table to another pivot table

    Great, romperstomper, your method worked.

    Somehow I tried to assign same index number to CacheIndex property but for no reason it failed. This time it worked. A big thank-you!

  6. #6
    Registered User
    Join Date
    10-28-2012
    Location
    Salt Lake City, USA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Thumbs up Re: VBA to change data source of pivot table to another pivot table

    Quote Originally Posted by MarvinP View Post
    Hi huyeole1,

    I recorded two macros that changed the data source for a single pivot table. See if the attached example helps you figure out how to solve your problem. I was unable to make the SourceData equal to a named range.
    Dude, Thank you! It worked!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1