I have a small little pivot table supported by 22,000 x 40 table. It has a simple autoformat applied along with some other custom formatting and headers. I want to send this pivot table without the supporting data because I don't want to send a 4MB Excel file for the sake of the 20k worth of pivot table I actually care about.
The problem is that if I select the pivot table and copy paste as values, the formatting gets erased. The only partial solution I have found is to copy the pivot table, paste it into a blank range as formats, then copy and paste the pivot table as values, then copy and paste back the formatting, then delete the temp range with the formatting. I could almost live with that convoluted way of doing things, but I run into a different problem doing it with a pivot table that has a report filter added. When you try to select the pivot including the report filter row, it simply refuses to paste the formats anywhere else. Now I have to copy and paste the pivot table formatting excluding the report filter into a blank range, the copy and paste the pivot tables as values, then copy back the formatting from the temp range, then manually apply the format to the report filter range.
What is the deal with this odd behavior? Why isn't there a simple one-click to convert a pivot table to values? I would think that would be a pretty common thing to do.
I have tried to code together a solution, but it leaves a lot to be desired. Basically what it does is copy the entire pivot table to a temp workbook, then change the pivot table to values, then copy and paste the formatting cell-by-cell back to the original pivot. It has to go cell-by-cell because of the issue with paste formats not working when there is a report filter. But cell-by-cell processing makes for slow execution on anything but the smallest pivot tables. And also, for good measure, I ran into another bug in which the paste formats does not go to the correct row unless screenupdating is turned on. I have yet to figure that one out.
Sub PivottoValuesKeepFormatting()
Dim bkNew As Workbook
Dim bkCur As Workbook
Dim r As Range
Dim r2 As Range
Dim l As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set r2 = Selection
r2.Copy
Set bkCur = ActiveWorkbook
Set bkNew = Workbooks.Add
Range("A1").PasteSpecial xlPasteAll
bkCur.Activate
Selection.PasteSpecial xlPasteValues
bkNew.Activate
Application.ScreenUpdating = True 'I don't know why, but the formats end up in the wrong place if you don't refresh the screen
Application.ScreenUpdating = False
For Each r In Selection
l = l + 1
r.Copy
bkCur.Activate
r2.Cells(l).PasteSpecial xlPasteFormats
bkNew.Activate
Next
bkNew.Close savechanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub