Specify Columns in Pivot Table

dashiellx

New Member
Joined
May 10, 2023
Messages
11
Office Version
  1. 365
  2. 2021
  3. 2016
  4. 2013
  5. 2010
  6. 2003 or older
Platform
  1. Windows
  2. Web
Pivot table creates like this:
Standard1 Day2 Days3 Days4 Days5 Days10+ Days8 Days

I need it to be:
Standard1 Day2 Days3 Days4 Days5 Days6 Days7 Days8 Days9 Days10+ Days

Even if there is no data for the age category

The following is correctly identifying the missing column, but is not creating it:

VBA Code:
varPivotItems(0) = "Standard"
varPivotItems(1) = "1 Day"
varPivotItems(2) = "2 Days"
varPivotItems(3) = "3 Days"
varPivotItems(4) = "4 Days"
varPivotItems(5) = "5 Days"
varPivotItems(6) = "6 Days"
varPivotItems(7) = "7 Days"
varPivotItems(8) = "8 Days"
varPivotItems(9) = "9 Days"
varPivotItems(10) = "10+ Days"
For Each varPivotItem In varPivotItems
    Set ptField = .PivotTables("piv" & strPivotSheetName).PivotFields("Age Category")
    bPivotItem = True
    For Each ptItem In ptField.PivotItems
        If ptItem.Name = varPivotItem Then
            bPivotItem = True
            Exit For
        Else
            bPivotItem = False
        End If
    Next ptItem
    If bPivotItem = False Then
        Debug.Print varPivotItem
       .PivotTables("piv" & strPivotSheetName).PivotFields("Age Category").PivotItems.Add varPivotItem
    End If
Next varPivotItem

However, this does not appear to be adding the item nor is the line erroring.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
The code was adding the pivottiems correctly, I just needed to add the following:

.PivotTables("piv" & strPivotSheetName).PivotFields("Age Category").ShowAllItems = True
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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