Richard has attended:
Excel VBA Intro Intermediate course
Pivot Tables - Orientation issues?
Hi there,
I am trying to write the correct procudure to create a pivot table. I have recorded a macro which does the job, as below;
Recorded Procedure:
Sub CreatePivotTable()
Sheets("AR134_VBA_Template").Select
Range("A8").Select
Selection.CurrentRegion.Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"AR134_VBA_Template!R1C1:R897C4").CreatePivotTable TableDestination:="", _
TableName:="PivotTable2"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
"Customer Name", "Data")
With ActiveSheet.PivotTables("PivotTable2").PivotFields("91-180 Days Past Due")
.Orientation = xlDataField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("180+ Days Past Due")
.Orientation = xlDataField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields("91 Days+").Orientation = _
xlDataField
Range("B3").Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Data")
.Orientation = xlColumnField
.Position = 1
End With
Application.CommandBars("PivotTable").Visible = False
End Sub
*Please notice the last part:
Range("B3").Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Data")
.Orientation = xlColumnField
.Position = 1
End With
I have then tried to write this 'macro' into a procedure and tried to change the PivotFields, e.g.("91 Days+") orientation from xlDataField to xlColumnField, as below;
Altered Procedure:
Sub CreatePivotTable()
Sheets("AR134_VBA_Template").Select
Range("A8").Select
Selection.CurrentRegion.Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"AR134_VBA_Template!R1C1:R897C4").CreatePivotTable TableDestination:="", _
TableName:="PivotTable2"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array( _
"Customer Name", "Data")
With ActiveSheet.PivotTables("PivotTable2").PivotFields("91-180 Days Past Due")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("180+ Days Past Due")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("91 Days+")
.Orientation = xlColumnField
.Position = 1
End With
Application.CommandBars("PivotTable").Visible = False
End Sub
The Recorded Procedure works just fine, but the Altered Procedure does not, it comes out as a blank pivot table, please could someone explain to me what im doing wrong and why? + I've also tried changing the 'Positions' of the column fields to 1, 2 & 3 respectively, but that doesnt work either. If you could let me know what the last bit of the recorded macro is doing, the
Range("B3").Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Data")
.Orientation = xlColumnField
.Position = 1
End With
bit.
I hope the above is possible to understand, I do appreciate this may be difficult without seeing the actual data being used.
Thanks for your time.
RE: Pivot Tables - Orientation issues?
Hi Richard
Thank you for your qustion.
It is very hard to understand your problem without seeing the workbook and how the code runs. Could you email me a copy of the workbook and I can then look into the problem for you.
My email is stephenATstl-training.co.uk
Regards
Stephen