PDA

View Full Version : Solved: Looping pivot tables



sassora
03-16-2008, 04:28 AM
I am trying to loop this so that I get about 20 pivot tables stacked on top of each other in a worksheet. I've tried without success, shouldn't this be simple to do?. Any ideas?

The fields I want to include in the other pivot tables are the same except the 1A will change to various other names.

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R8324C12").CreatePivotTable TableDestination:= _
"'[Judgements 0607.xls]Test'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:=Array( _
"Care Region", "CORegion"), ColumnFields:=Array( _
"Phase", "1A")
ActiveSheet.PivotTables("PivotTable1").PivotFields("1A").Orientation = _
xlDataField

herzberg
03-16-2008, 11:52 PM
I'm not sure if this is what you want but here goes:
Dim CustomPivot As PivotTable
Dim PivotArray(3) As String
Dim Counter As Long

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R8324C12").CreatePivotTable TableDestination:= _
"'[Judgements 0607.xls]Test'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

Set CustomPivot = ActiveSheet.PivotTables("PivotTable1")
PivotArray(1) = "Apple"
PivotArray(2) = "Bear"
PivotArray(3) = "Carbon"

With CustomPivot
For Counter = 1 To 3
.AddFields RowFields:=Array( _
"Care Region", "CORegion"), ColumnFields:=Array( _
"Phase", PivotArray(Counter))
.PivotFields(PivotArray(Counter)).Orientation = xlDataField
'Some other code before the next item is added
Next Counter
End With
Just expand the array to include the fields that you want to be added into the pivot table.