Pivot Table Sort by Data Column

VBA learner ITG

Active Member
Joined
Apr 18, 2017
Messages
267
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have used the below VBA code from other post to generate a test Pivot Table and now I have been asked to sort the Data which is the Values Column by Highest Number in descending Order.

I have tried to amend the code but i keep getting error messages with different VBA codes i have tried.

can you help solve my conundrum at all?




Dim ws As Worksheet, pc As PivotCache, pt As PivotTable
Set ws = Worksheets.Add
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, "Data!A1:M2000")
Set pt = pc.CreatePivotTable(ws.Range("A3"))
'Setting Fields
With pt
With .PivotFields("Project Manager")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Project Cost")
.Orientation = xlColumnField
.Position = 1
End With
.AddDataField .PivotFields("Project Cost"), "Sum of Print Cost", xlSum
End With

 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Here's some code you can try...

Code:
Sub MakePivotThenSortValues()
 Dim ws As Worksheet, pc As PivotCache, pt As PivotTable
 
 Set ws = Worksheets.Add
 Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, "Data!A1:M2000")
 Set pt = pc.CreatePivotTable(ws.Range("A3"))
 
 'Setting Fields
 With pt
   With .PivotFields("Project Manager")
      .Orientation = xlRowField
      .Position = 1
   End With
   
   .AddDataField .PivotFields("Project Cost"), "Sum of Print Cost", xlSum
 
   .PivotFields("Project Manager").AutoSort _
        xlDescending, "Sum of Print Cost"

 End With

End Sub

I've eliminated the part of your code in which you added Project Cost as a Column Field. I'm assuming that you were just trying to show the values in a single column instead of having a separate column for each unique Project Cost value.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,733
Members
449,465
Latest member
TAKLAM

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