As an Amazon associate we earn from qualifying purchases. Thanks for your support!                               
×

Best Blu-ray Movie Deals


Best Blu-ray Movie Deals, See All the Deals »
Top deals | New deals  
 All countries United States United Kingdom Canada Germany France Spain Italy Australia Netherlands Japan Mexico
Alfred Hitchcock: The Ultimate Collection 4K (Blu-ray)
$124.99
6 hrs ago
Superman I-IV 5-Film Collection 4K (Blu-ray)
$74.99
1 day ago
Karate Kid: Legends 4K (Blu-ray)
$24.97
9 hrs ago
The Howling 4K (Blu-ray)
$35.99
1 day ago
How to Train Your Dragon 4K (Blu-ray)
$39.95
6 hrs ago
The Rage: Carrie 2 4K (Blu-ray)
$28.99
6 hrs ago
Nobody 2 (Blu-ray)
$22.95
1 hr ago
A Confucian Confusion / Mahjong: Two Films by Edward Yang (Blu-ray)
$36.69
4 hrs ago
American Pie 4K (Blu-ray)
$23.79
3 hrs ago
Superman 4K (Blu-ray)
$29.95
 
Back to the Future Part III 4K (Blu-ray)
$24.99
 
Jurassic World: 7-Movie Collection 4K (Blu-ray)
$99.99
 
What's your next favorite movie?
Join our movie community to find out


Image from: Life of Pi (2012)

Go Back   Blu-ray Forum > Entertainment > General Chat
Register FAQ Community Calendar Today's Posts Search


Reply
 
Thread Tools Display Modes
Old 06-26-2013, 06:53 PM   #1
kpkelley kpkelley is offline
Blu-ray Ninja
 
kpkelley's Avatar
 
Aug 2007
Framingham, MA
385
2478
113
152
Default Pivot Table from Macro in Excel

I'm trying to create a macro that will output a pivot table on the same sheet as the data from which the pivot table was created. I'm running in to a few errors.

Firstly, I get an error message every time I try to run the macro and it highlights this sections as being the culprit:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDa tabase, SourceData:= _
"Report 005!R1C1:R100000C41", Version:=xlPivotTableVersion12). _
CreatePivotTable TableDestination:="Report 005!R2C53", TableName:= _
"PivotTable2", DefaultVersion:=xlPivotTableVersion12

I can bypass the issue by changing TableDestination from "Report 005!R2C53" to "", but then the pivot table gets created in another sheet. Do you know of an alternative?

The other issue that I've encountered is that some of the changes I made to the Pivot Table when I was recording it for one set of data, cause errors on the second second set of data. On the first data set I deselected several instruments from the "instrument" row in the pivot table as seen in this code:
With ActiveSheet.PivotTables("PivotTable2").PivotFields ("Instrument")
.PivotItems("C").Visible = False
.PivotItems("D").Visible = False
.PivotItems("R").Visible = False
.PivotItems("(blank)").Visible = False.
End With

However, the second data set doesn't have any Instrument type D data points. Is there a way to avoid the error message if a data set doesn't have that Instrument Type?

TIA

Last edited by kpkelley; 06-26-2013 at 07:42 PM.
  Reply With Quote
Old 06-27-2013, 12:58 AM   #2
Anthony P Anthony P is offline
Blu-ray Count
 
Jul 2007
Montreal, Canada
Default

I think your first issue is a simple one. If you recorded the macro (and from what you pasted it looks like you did) than a pivot table already exists at Report 005!R2C53 so you would need to delete the one that is there before you begin that is why it lets you create it on a new page since there is never a pivot table on a blank new page

for the second one the easiest thing to do is

With ActiveSheet.PivotTables("PivotTable2").PivotFields ("Instrument")
On error resume next
.PivotItems("C").Visible = False
.PivotItems("D").Visible = False
.PivotItems("R").Visible = False
.PivotItems("(blank)").Visible = False.
On error goto 0
End With

in essence the first bold line means that if there is an error just skip that line and in this case it makes sense, no use hiding data that is not there in the first place. It is also good to stop it right after so if there are other errors they will get traped
  Reply With Quote
Old 06-27-2013, 02:28 PM   #3
kpkelley kpkelley is offline
Blu-ray Ninja
 
kpkelley's Avatar
 
Aug 2007
Framingham, MA
385
2478
113
152
Default

Thanks for the response.

I'll try the second one shortly, but the first one isn't the problem that I'm running into. I've deleted all the data on the sheet and replaced it, including taking out the pivot table.

One idea that I saw on another forum, was that it had to do with the pivot table name. Since a pivot table of that name already existed, it couldn't create another one. The solution was a string integer with the pivot table name, but I don't know how to write that.

TIA
  Reply With Quote
Old 06-27-2013, 02:59 PM   #4
kpkelley kpkelley is offline
Blu-ray Ninja
 
kpkelley's Avatar
 
Aug 2007
Framingham, MA
385
2478
113
152
Default

I got it to work in a new sheet with the on error resume, by changing the TableDestiantion to "".

Thanks.
  Reply With Quote
Reply
Go Back   Blu-ray Forum > Entertainment > General Chat



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

Forum Jump


All times are GMT. The time now is 09:20 PM.