Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a macro to create a pivot table from variable size data? 1

Status
Not open for further replies.

daggers

Technical User
Sep 10, 2002
13
GB
Hi,

I am trying to write a macro that will create a pivot table from a list in an excel worksheet. The list will change in size every time I want the macro run.

Range("B1:D25").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _"'3M'!R1C2:R25C4").CreatePivotTable TableDestination:="", TableName:= _"PivotTable1"

This is the part of the macro I have a problem with - If x and y is the top left corner of the list, and xx and yy is the bottom right corner of the list, then Range(Cells(y,x),Cells(yy,xx)).Select should replace the first line...but how do I change the 'SourceData' property to reflect variables instead of constants?

TIA
Ross
 
This should do the trick:

dim srcRng as range
set srcRng = sheets("Sheetname").range(cells(x,y),cells(xx,yy))

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= srcRng).CreatePivotTable TableDestination:="", TableName:= _"PivotTable1"

HTH Rgds
~Geoff~
 
Unfortunately, I get the error message "Application-defined or Object-Defined Error" using the code:

set srcRng = Sheets("Sheetname").Range(Cells(x,y),Cells(xx,yy))

And if I replace Range(Cells(x,y),Cells(xx,yy)) with Range("A2:D25"), I then get the error message "Invalid procedure call or argument" with reference to the code:

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= srcRng).CreatePivotTable TableDestination:="", TableName:= _"PivotTable1"

So much fun! Any other ideas?

Thanks
Ross
 
Well, this worked for me (xl97)

Dim srcRng As Range
Set srcRng = Sheets("Sheet1").Range(Cells(1, 1), Cells(215, 2))


ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:=srcRng, TableDestination:="R1C4", TableName:="PivotTable1"
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="Product Name"
ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Number if Sites in the set with Product").Orientation = xlDataField

Presumably you use 2000 / 2oo2 if you are using the PivotCache method - eiother way, the range reference should work the same Rgds
~Geoff~
 
May need to check that Tools/References in the VB Editor are the same. (??)

Cannot see why this should be though. Regards
BrianB
** Let us know if you get something that works !
================================
 
I got it to work last night using the code in Geoff's second post. It is (works for Excel 2000):

Dim srcRng As Range
Set srcRng = Sheets("worksheet").Range(Cells(1,2), Cells(x,4))

ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:=srcRnga, TableDestination:="'DestWSheet'!R1C1", TableName:="PivotTable1"
ActiveSheet.PivotTables("PivotTable1").SmallGrid=False
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="columndata", _ ColumnFields:="rowdata"
ActiveSheet.PivotTables("PivotTable1").PivotFields("sumofdata").Orientation = _ xlDataField


as Geoff said, not sure why Range(cells) did not work but it worked last night.

Thanks for your help
Ross
 
Well, if you used my exact code, you used the PivotTableWIZARD method rather than the PivotCACHE method you had in your 1st post..... Rgds
~Geoff~
 
?? not sure what you mean - I got it to work by using PivotTableWizard rather than PivotCaches (which the recorder will generate in Excel 2000)

If you look at my code in my last post it is using the PivotTableWizard method.

Ross
 
That's what I mean - you got it working with the WIZARD method but not the CACHES method which was part of your 1st post. The code you posted in response to my 1st post still used the cache method and that's probably why it failed. The learning point here may be to go to "Help" and find out what the differences between the pivotcache and pivottablewizard method are. I would, but I can't 'cos I only have xl97 Rgds
~Geoff~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top