+ Reply to Thread
Results 1 to 2 of 2

Thread: Changing PivotTable page field in a macro

  1. #1
    Registered User
    Join Date
    12-07-2011
    Location
    indiana
    MS-Off Ver
    Excel 2010
    Posts
    1

    Changing PivotTable page field in a macro

    I am having trouble with a macro in Excel 2010 that used to work just fine in Excel 2003.

    The problem is with changing a PivotTable Page field in the macro. An example of the code is below.

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Year").CurrentPage = year1
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Year").CurrentPage = year1 - 1

    I am changing the value in the Page field, which changes the values in the pivot table. This works just fine as is.

    When I add in a line to refresh the pivot cache between changing the year, then I get an error.

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Year").CurrentPage = year1
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Year").CurrentPage = year1 - 1

    After I refresh the cache, then when I try to change the Page field again, I get an error:

    'Run-time error '5': Invalid procedure call or argument'

    Anybody have any ideas?

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Changing PivotTable page field in a macro

    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    I dont think this line is required. Once you select the value for the Page and then change it again, the pivot gets refreshed automatically. Try without the above line and see if you get any errors.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0