Re-organizing pivot table fields in vba

ummjay

Board Regular
Joined
Oct 1, 2010
Messages
193
Hi! how can I reorganize the order of the pivot fields: Client Type, Product, Side. When I run the below, the rows are in the following order: side, product, client type:

VBA Code:
'START pivot
 ActiveSheet.UsedRange.Select
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        Sheets(1).UsedRange).CreatePivotTable TableDestination:="", _
        TableName:="Pivot Summary", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
'Insert Side to Row Filed & position 2
With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Client Type")
        .Orientation = xlRowField
        .Position = 1
    End With
  'Insert Side to Row Filed & position 2
With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Product")
        .Orientation = xlRowField
        .Position = 1
    End With
  'Insert Side to Row Filed & position 2
  With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Side")
    .Orientation = xlRowField
    .Position = 1
    End With
'Insert Qty column to the data field
  With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Volume")
    .Orientation = xlDataField
    .Position = 1
  .NumberFormat = "#,##0;(#,##0)"
  
  ActiveWorkbook.RefreshAll
    'Turn on Automatic updates/calculations --like screenupdating to speed up code
    pvt.ManualUpdate = False
  
  End With
  
Application.DisplayAlerts = True
Application.ScreenUpdating = True
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi @ummjay. Thanks for posting on the board.

Just update the position number:

Rich (BB code):
  'Insert Side to Row Filed & position 1
  With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Client Type")
    .Orientation = xlRowField
    .Position = 1
  End With
  'Insert Side to Row Filed & position 2
  With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Product")
    .Orientation = xlRowField
    .Position = 2
  End With
  'Insert Side to Row Filed & position 3
  With ActiveSheet.PivotTables("Pivot Summary").PivotFields("Side")
    .Orientation = xlRowField
    .Position = 3
  End With

The previous lines can be simplified like this
VBA Code:
  With ActiveSheet.PivotTables("Pivot Summary")
    .PivotFields("Client Type").Orientation = xlRowField
    .PivotFields("Client Type").Position = 1
    .PivotFields("Product").Orientation = xlRowField
    .PivotFields("Product").Position = 2
    .PivotFields("Side").Orientation = xlRowField
    .PivotFields("Side").Position = 3
  End With


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Upvote 0
Solution

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top