Good morning.
I have a routine that is exporting data to Excel and creating a pivot table.
The column header for the pivot is "Age Group" and should always be "<5 Days", "5 - 10 Days", "11 - 20 Days", and ">20 Days" even if there is no data for the age group.
Excel obviously doesn't order those columns correctly, so I have the following code to make sure they are in the correct order
Code:
.PivotTables(spivTableName).PivotFields("AgeGroup").ShowAllItems = True
.PivotTables(spivTableName).NullString = "0"
.PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("<5 Days").Position = 1
.PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("5 - 10 Days").Position = 2
.PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("11 - 20 Days").Position = 3
.PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems(">20 Days").Position = 4
Which works great as long as there is data for each of the columns; but if there is no data for one of the Age Groups, a 'Run-Time error 1004 Unable to get the PivotItem property of the PivotFields' is raised.
How can I set the column headers to show even for no data in the data source range for that column?