Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

pivot tables orientation

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Pivot Tables - Orientation issues?

Pivot Tables - Orientation issues?

ResolvedVersion 2003

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

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Using Alt in Save Dialog Box

When you are saving (or opening) a file, try these;
(Note the numbers are on the keyboard not the numeric keypad)
1. Go to previous folder Alt+1
2. Go up one folder level Alt+2
3. Search the Web Alt+3
4. Delete selected file Alt+4 or Delete
5. Create a new folder Alt+5
6. Cycle through all views Alt+6 repeatedly
7. Display the Tools menu Alt+7

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.