Home » excel » excel – Copying Pivot Table Formats

excel – Copying Pivot Table Formats

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m having trouble getting pivot table formats to copy to a new sheet. Basically what I’m trying to do is this:

someRange.Copy
someOtherRange.pasteSpecial xlPasteValues
someOtherRange.pasteSpecial xlPasteFormats

Basically, I want an exact copy of the current view on a pivot table in a new sheet but I just want the values and formatting (and not be connected to the pivot table any longer). Doing this procedure by hand produces the correct result, but for some reason the formatting doesn’t copy when I use the same approach via vba… Thoughts?

Update:
Exact code as requested:

Application.CutCopyMode = False
pivotSheet.Range(pivotSheet.usedRange.Cells(1, 2), pivotSheet.Cells(pivotSheet.usedRange.Rows.Count, pivotSheet.Columns.Count)).Copy
updatesSheet.Range(updatesSheet.Cells(1, 1), updatesSheet.Cells(pivotSheet.usedRange.Rows.Count, pivotSheet.usedRange.Columns.Count)).PasteSpecial xlPasteValues
updatesSheet.Range(updatesSheet.Cells(1, 1), updatesSheet.Cells(pivotSheet.usedRange.Rows.Count, pivotSheet.usedRange.Columns.Count)).PasteSpecial xlPasteFormats
Application.CutCopyMode = False
How to&Answers:

TRIED AND TESTED

Option Explicit

Sub Sample()
    With Sheets("Sheet1")
        .Range("H1:I6").Copy

        With .Range("M7")
            .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

            .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        End With
    End With
End Sub

SNAPSHOT

enter image description here

Answer:

Before copying and pasting manually, start recording a macro. After you’re finished, stop the recording and examine the recorded code. You’ll get a bunch of extra stuff you can delete, but you’ll also get the correct syntax for doing what you’re trying to do.