Here you go. There was a slight curve. The column headers can vary in both name and number.
The key to this code is the definition of a pair of named dynamic rangesPivot_Copy =OFFSET(Pivot!$A$5,0,0,COUNTA(Pivot!$A:$A)-3,COUNTA(Pivot!$4:$4))
Pivot_Headers =OFFSET(Pivot!$A$4,0,0,1,COUNTA(Pivot!$4:$4))
The offset command has 5 arguments: a starting cell, how many cells to go down, how many rows to go right and the number of rows and columns to return.
So =OFFSET(Pivot!$A$5,0,0,COUNTA(Pivot!$A:$A)-3,COUNTA(Pivot!$4:$4)) means
Start in A5
Go down zero rows
Go right zero columns
Return COUNTA(A:A)-3 rows (-3 because we don't want to count headers and the Grand Total row)
Return COUNTA(Pivot!$4:$4) columns
The VB code removes all columns but the first and then clears the table and then copy in the column headers and the body of the pivot table.
Bookmarks