Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

How to copy a pivot as values without losing formatting

Latest post Sun, Jun 29 2008 8:00 PM by Debra Dalgleish. 1 replies.
  • Fri, Jun 27 2008 2:21 PM

    • zfraile
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Posts 37
    • Points 777

    How to copy a pivot as values without losing formatting

    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

    • Post Points: 21
  • Sun, Jun 29 2008 8:00 PM In reply to

    Re: How to copy a pivot as values without losing formatting

     Copy the pivot table into another workbook.

    In the new workbook, right-click on a cell in the pivot table, and click on PivotTable Options

    On the Data tab, remove the check mark for Save Source Data with File.

    This will preserve the pivot table, but not the data, so it should look the same, but would be non-functioning.

    • Post Points: 5
Page 1 of 1 (2 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.