PC Review


Reply
Thread Tools Rate Thread

Change Page Field in a Pivot Table and Print Chart + Data

 
 
=?Utf-8?B?UGhpbGlwIEogU21pdGg=?=
Guest
Posts: n/a
 
      14th Feb 2007
Hi I have a chart dependant upon a pivot table.

There is a page field which enables selection of Fuel Type. There are three
fuel types "Gas", "Electric" and "(All)".

I want to run a macro which selects a fuel type, prints the chart and data
and then selects the the next Fuel.

I recorded a simple macro to do it once and then tried to hack it to repeat
for the other fuels - the code is given below.

Sub PrintAnnual()
'
' Macro to print the Charts and Data for Annual Mag Card Holders
'
' Print Details for Electricity
ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
"Electric"
Sheets(Array("Annual Chart", "Annual Summary")).Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
' Print Details for Gas
ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
"Gas"
Sheets(Array("Annual Chart", "Annual Summary")).Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
' Print Details for (All)
ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
"(All)"
Sheets(Array("Annual Chart", "Annual Summary")).Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
' Return to Contents Sheet
Sheets("Workbook Contents").Select
End Sub

When I tested it I received the following error report.

Run time error 1004.
"Unable to get the pivot tables property of the worksheet class"

I would appreciate it if someone could identify how to correct the code so
that it works properly. Also is there a more elegant way of doing this?

Regards


 
Reply With Quote
 
 
 
 
=?Utf-8?B?ZGtpbm4=?=
Guest
Posts: n/a
 
      14th Feb 2007
My first question is:
Is the sheet that contains the pivot table the active sheet when the macro
is run?

The code is referencing ActiveSheet.PivotTables.

You may try to reference it directly like
Sheets("MyPivotTableSheet").PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
"Electric"
to eliminate the possibility of another sheet being active at the time the
code is run

First step is to get the code to run then we can try to simplify the code

best luck

David

where "MyPivotTableSheet" is the name of your worksheet



"Philip J Smith" wrote:

> Hi I have a chart dependant upon a pivot table.
>
> There is a page field which enables selection of Fuel Type. There are three
> fuel types "Gas", "Electric" and "(All)".
>
> I want to run a macro which selects a fuel type, prints the chart and data
> and then selects the the next Fuel.
>
> I recorded a simple macro to do it once and then tried to hack it to repeat
> for the other fuels - the code is given below.
>
> Sub PrintAnnual()
> '
> ' Macro to print the Charts and Data for Annual Mag Card Holders
> '
> ' Print Details for Electricity
> ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
> "Electric"
> Sheets(Array("Annual Chart", "Annual Summary")).Select
> ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
> ' Print Details for Gas
> ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
> "Gas"
> Sheets(Array("Annual Chart", "Annual Summary")).Select
> ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
> ' Print Details for (All)
> ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
> "(All)"
> Sheets(Array("Annual Chart", "Annual Summary")).Select
> ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
> ' Return to Contents Sheet
> Sheets("Workbook Contents").Select
> End Sub
>
> When I tested it I received the following error report.
>
> Run time error 1004.
> "Unable to get the pivot tables property of the worksheet class"
>
> I would appreciate it if someone could identify how to correct the code so
> that it works properly. Also is there a more elegant way of doing this?
>
> Regards
>
>

 
Reply With Quote
 
=?Utf-8?B?UGhpbGlwIEogU21pdGg=?=
Guest
Posts: n/a
 
      19th Feb 2007
Thanks for this, the macro now runs. I was running the macro from another
sheet and this was causing the problem. Sorry for the delay in responding
but I was diverted to another problem.

Regards

Phil

"dkinn" wrote:

> My first question is:
> Is the sheet that contains the pivot table the active sheet when the macro
> is run?
>
> The code is referencing ActiveSheet.PivotTables.
>
> You may try to reference it directly like
> Sheets("MyPivotTableSheet").PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
> "Electric"
> to eliminate the possibility of another sheet being active at the time the
> code is run
>
> First step is to get the code to run then we can try to simplify the code
>
> best luck
>
> David
>
> where "MyPivotTableSheet" is the name of your worksheet
>
>
>
> "Philip J Smith" wrote:
>
> > Hi I have a chart dependant upon a pivot table.
> >
> > There is a page field which enables selection of Fuel Type. There are three
> > fuel types "Gas", "Electric" and "(All)".
> >
> > I want to run a macro which selects a fuel type, prints the chart and data
> > and then selects the the next Fuel.
> >
> > I recorded a simple macro to do it once and then tried to hack it to repeat
> > for the other fuels - the code is given below.
> >
> > Sub PrintAnnual()
> > '
> > ' Macro to print the Charts and Data for Annual Mag Card Holders
> > '
> > ' Print Details for Electricity
> > ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
> > "Electric"
> > Sheets(Array("Annual Chart", "Annual Summary")).Select
> > ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
> > ' Print Details for Gas
> > ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
> > "Gas"
> > Sheets(Array("Annual Chart", "Annual Summary")).Select
> > ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
> > ' Print Details for (All)
> > ActiveSheet.PivotTables("PivotTable2").PivotFields("FUEL").CurrentPage =
> > "(All)"
> > Sheets(Array("Annual Chart", "Annual Summary")).Select
> > ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
> > ' Return to Contents Sheet
> > Sheets("Workbook Contents").Select
> > End Sub
> >
> > When I tested it I received the following error report.
> >
> > Run time error 1004.
> > "Unable to get the pivot tables property of the worksheet class"
> >
> > I would appreciate it if someone could identify how to correct the code so
> > that it works properly. Also is there a more elegant way of doing this?
> >
> > Regards
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 - cannot print Page field item on a Pivot Chart? SallyC Microsoft Excel Discussion 0 9th Apr 2009 01:36 PM
Pivot Table - change page field help Opal Microsoft Excel Programming 23 16th Mar 2009 07:07 PM
Change the name of the Data field in a Pivot Table John Microsoft Excel Misc 2 6th Oct 2008 05:16 PM
HDI: Pivot Chart page data change per slide vbasean Microsoft Powerpoint 0 1st Aug 2008 04:21 PM
Auto-print Pivot Table Report that cycles through all Page Field values... Tripp Knightly Microsoft Excel Misc 1 28th Jul 2004 02:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:39 AM.