Sorting in pivot table

theteacher9999

New Member
Joined
Jun 8, 2020
Messages
12
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I'm trying to solve a problem, so far without success. I have a pivot table and , with VBA code, I would like the values in the 'Country' field to be in the exact order I specified:
(blank), Czech Republic, Finland, Denmark, Norway, Spain, Greece, Brazil, Argentina

This is what I have:
1628709754538.png


I know Norway isn't in my table, but if it does show up, it should be in the right place after running the code.

I was trying to achieve my goal using the code below but with no luck.

VBA Code:
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet4").PivotTables("PivotTable2")

Application.AddCustomList Array("(blank)", "Czech Republic", "Finland", "Denmark", "Spain", "Greece", "Brazil", "Argentina")

PvtTbl.SortUsingCustomLists = True

PvtTbl.PivotFields("Country").DataRange.Sort Order1:=xlAscending, Type:=xlSortLabels

BTW, when I run it and then I sort the column manually (A to Z), I get the below:

1628710838338.png


It seems to be fine except "(blank)" which is not at the beginning. I am confused. Do you have an idea how to solve it?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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