Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /usr/www/users/mrexcel/website_configuration.php on line 10

Your One Stop for Excel Tips & Solutions


MrExcel - Photos of MrExcel

Excel Pivot Table Printing Every Value in the Page Field Using a Macro

Raj asks: I have an Excel pivot table set up with 150 different values in the page field. How can I run a macro to print each individual page? I tried recording the macro, but it hard codes the name of each page, and some of those pages may not be there from week to week.

Visual Basic makes this a snap. This macro uses the following:

  • The PageFields collection will tell you the name of the first page field in the pivot table.
  • The PivotItems collection is a list of all the items (pages) in the page field.
  • The CurrentPage property allows you to change which page is displayed in the pivot table.
Sub PrintAll()
    ' Find name of page field
    PageField1 = ActiveSheet.PivotTables("PivotTable1").PageFields(1)
    ' Save the name of the page field displayed when macro begins
    OrigPage = ActiveSheet.PivotTables("PivotTable1").PivotFields(PageField1).CurrentPage
    ' Determine how many pages there are in page field
    NumPages = ActiveSheet.PivotTables("PivotTable1").PivotFields(PageField1).PivotItems.Count
    For i = 1 To NumPages
        ThisPage = ActiveSheet.PivotTables("PivotTable1").PivotFields(PageField1).PivotItems(i)
        ActiveSheet.PivotTables("PivotTable1").PivotFields(PageField1).CurrentPage = ThisPage
    Next i
    ' Print the (all) page
    ActiveSheet.PivotTables("PivotTable1").PivotFields(PageField1).CurrentPage = "(all)"
    ' Restore view back to original page
    ActiveSheet.PivotTables("PivotTable1").PivotFields(PageField1).CurrentPage = OrigPage
End Sub

A tip of the cap to Raj for reporting back with this hint. The above macro will not work when you have your pivot table sorted under Advanced. Resetting this to manual will correct the problem.

Note: This tip will loop through every page field in order to print them. If you want to generate a workbook with 150 different worksheets, one for each page field value, you can use the Show Pages feature. This feature is described in several places at MrExcel.com:

Warning: mysql_connect(): Access denied for user 'mrexcel_22_w'@'localhost' (using password: YES) in /usr/www/users/mrexcel/siteadmin/admincp/config.php on line 8
Could not connect