PC Review


Reply
Thread Tools Rate Thread

Avoiding an extra sheet when creating a pivot table

 
 
SteveM
Guest
Posts: n/a
 
      28th Mar 2009
I have this code now:
ActiveSheet.Cells(1, 1).Select
ActiveCell.CurrentRegion.Select
srcdata = ActiveCell.CurrentRegion.Address

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
srcdata).CreatePivotTable TableDestination:="" _
, TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10

ActiveSheet.PivotTableWizard TableDestination:="Counts!R3C1"

ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
"FeatureName", "State"), ColumnFields:="Severity"

ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
xlDataField


Which inserts an extra sheet. What would seem logical to me is to give the
table destination a value in the first statement instead of adding it as its
own line. So I thought I could do this:
ActiveSheet.Cells(1, 1).Select
ActiveCell.CurrentRegion.Select
srcdata = ActiveCell.CurrentRegion.Address

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
, TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10

ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
"FeatureName", "State"), ColumnFields:="Severity"

ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
xlDataField


But then I get an error when it reaches the next statement setting the row
and column fields. How do I give a destination in the first statement? Or is
there a way to combine all of it into one statement?

Thanks in advance,
S.
 
Reply With Quote
 
 
 
 
joel
Guest
Posts: n/a
 
      28th Mar 2009
You don't know the name of the pivot table that is being added. that is why
you are getting the error. Try this

with ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
, TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10

.AddFields RowFields:=Array( _
"FeatureName", "State"), ColumnFields:="Severity"

.PivotFields("id").Orientation = _
xlDataField
end with


"SteveM" wrote:

> I have this code now:
> ActiveSheet.Cells(1, 1).Select
> ActiveCell.CurrentRegion.Select
> srcdata = ActiveCell.CurrentRegion.Address
>
> ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> srcdata).CreatePivotTable TableDestination:="" _
> , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
>
> ActiveSheet.PivotTableWizard TableDestination:="Counts!R3C1"
>
> ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
> "FeatureName", "State"), ColumnFields:="Severity"
>
> ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
> xlDataField
>
>
> Which inserts an extra sheet. What would seem logical to me is to give the
> table destination a value in the first statement instead of adding it as its
> own line. So I thought I could do this:
> ActiveSheet.Cells(1, 1).Select
> ActiveCell.CurrentRegion.Select
> srcdata = ActiveCell.CurrentRegion.Address
>
> ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
> , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
>
> ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
> "FeatureName", "State"), ColumnFields:="Severity"
>
> ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
> xlDataField
>
>
> But then I get an error when it reaches the next statement setting the row
> and column fields. How do I give a destination in the first statement? Or is
> there a way to combine all of it into one statement?
>
> Thanks in advance,
> S.

 
Reply With Quote
 
StevenM
Guest
Posts: n/a
 
      28th Mar 2009
Thanks. I tried substituting what I had for what you coded below, but when I
do, the first section:
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
, TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10

is in red with an error message saying "compile error: expected end of
statement" and the TableDestination is highlighted.

Any idea what I can try next?



"joel" wrote:

> You don't know the name of the pivot table that is being added. that is why
> you are getting the error. Try this
>
> with ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
> , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
>
> .AddFields RowFields:=Array( _
> "FeatureName", "State"), ColumnFields:="Severity"
>
> .PivotFields("id").Orientation = _
> xlDataField
> end with
>
>
> "SteveM" wrote:
>
> > I have this code now:
> > ActiveSheet.Cells(1, 1).Select
> > ActiveCell.CurrentRegion.Select
> > srcdata = ActiveCell.CurrentRegion.Address
> >
> > ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> > srcdata).CreatePivotTable TableDestination:="" _
> > , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
> >
> > ActiveSheet.PivotTableWizard TableDestination:="Counts!R3C1"
> >
> > ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
> > "FeatureName", "State"), ColumnFields:="Severity"
> >
> > ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
> > xlDataField
> >
> >
> > Which inserts an extra sheet. What would seem logical to me is to give the
> > table destination a value in the first statement instead of adding it as its
> > own line. So I thought I could do this:
> > ActiveSheet.Cells(1, 1).Select
> > ActiveCell.CurrentRegion.Select
> > srcdata = ActiveCell.CurrentRegion.Address
> >
> > ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> > srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
> > , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
> >
> > ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
> > "FeatureName", "State"), ColumnFields:="Severity"
> >
> > ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
> > xlDataField
> >
> >
> > But then I get an error when it reaches the next statement setting the row
> > and column fields. How do I give a destination in the first statement? Or is
> > there a way to combine all of it into one statement?
> >
> > Thanks in advance,
> > S.

 
Reply With Quote
 
StevenM
Guest
Posts: n/a
 
      28th Mar 2009
Forget my previous note. I tracked down the problem to missing parentheses
after the CreatePivotTable piece. I ended up with this:

With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
srcdata).CreatePivotTable(TableDestination:="Counts!R3C1", _
TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion10)

.AddFields RowFields:=Array( _
"FeatureName", "State"), ColumnFields:="Severity"

.PivotFields("id").Orientation = _
xlDataField
End With

And that seemed to do it. Thank you for pointing me in the right direction.
s.

"joel" wrote:

> You don't know the name of the pivot table that is being added. that is why
> you are getting the error. Try this
>
> with ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
> , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
>
> .AddFields RowFields:=Array( _
> "FeatureName", "State"), ColumnFields:="Severity"
>
> .PivotFields("id").Orientation = _
> xlDataField
> end with
>
>
> "SteveM" wrote:
>
> > I have this code now:
> > ActiveSheet.Cells(1, 1).Select
> > ActiveCell.CurrentRegion.Select
> > srcdata = ActiveCell.CurrentRegion.Address
> >
> > ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> > srcdata).CreatePivotTable TableDestination:="" _
> > , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
> >
> > ActiveSheet.PivotTableWizard TableDestination:="Counts!R3C1"
> >
> > ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
> > "FeatureName", "State"), ColumnFields:="Severity"
> >
> > ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
> > xlDataField
> >
> >
> > Which inserts an extra sheet. What would seem logical to me is to give the
> > table destination a value in the first statement instead of adding it as its
> > own line. So I thought I could do this:
> > ActiveSheet.Cells(1, 1).Select
> > ActiveCell.CurrentRegion.Select
> > srcdata = ActiveCell.CurrentRegion.Address
> >
> > ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> > srcdata).CreatePivotTable TableDestination:="Counts!R3C1" _
> > , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
> >
> > ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
> > "FeatureName", "State"), ColumnFields:="Severity"
> >
> > ActiveSheet.PivotTables("PivotTable2").PivotFields("id").Orientation = _
> > xlDataField
> >
> >
> > But then I get an error when it reaches the next statement setting the row
> > and column fields. How do I give a destination in the first statement? Or is
> > there a way to combine all of it into one statement?
> >
> > Thanks in advance,
> > S.

 
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
How do I add extra data to a pivot table? Max Microsoft Excel Misc 3 21st Apr 2010 01:41 PM
extra data for pivot table Fawn Microsoft Excel Misc 0 15th Sep 2008 07:17 AM
Coded to delete extra rows after pivot table klysell Microsoft Excel Programming 2 11th Feb 2008 03:51 PM
How to pull extra info into Pivot table report =?Utf-8?B?TUxL?= Microsoft Excel Worksheet Functions 4 22nd Aug 2006 01:18 AM
Help required with setting up a pivot table with the source on sheet1 to have the pivot table created on sheet called "report" Diana Microsoft Excel Programming 0 21st Aug 2003 10:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:08 PM.