copy paste pivot table values and formatting

pliskers

Active Member
Joined
Sep 26, 2002
Messages
461
Office Version
  1. 2016
Platform
  1. Windows
Is someone familiar with the use of the TableRange2 command? I understand it can be used to copy and paste a pivot table's values and formatting onto itself. Normal commands work on values, but not formats.

Thank you!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi,

you have to copy and paste the pivot table twice. First the values and then the Formats. If your Pivot table is in Cell A1 you can use this code. Otherwise please amend the code to your needs.

Code:
Sub CopyPivotValuesAndFormats()
    With [A1]
        '***Values
        .PivotTable.TableRange2.Copy
        .Offset(0, 10).PasteSpecial xlPasteValues
        '***Formats
        .PivotTable.TableRange2.Copy
        .Offset(0, 10).PasteSpecial xlPasteFormats
    End With
End Sub
 
Upvote 0
A slight issue is that I'm copying/pasting the pivot table onto itself, so once we copy/paste the values, the formats disappear.

The pivot table to be copy/pasted will have been copied from another sheet, named Pivot.

Could you possibly suggest some coding that would copy the formatting from the original (on the Pivot sheet) and then return to the current sheet to paste the formatting?

Thanks again!
 
Upvote 0
Sorry, but I just checked and the code is not pasting formats, only values. I am using Excel 2007 - does that impact anything? Is there a setting that needs to be applied, or some other change to the code?
 
Upvote 0
This code should solve your problem. :biggrin:

Code:
Sub CopyPivotValuesAndFormats()
    Dim WKS As Worksheet
    Dim PT As PivotTable
    Set PT = ActiveSheet.Range("A1").PivotTable
    Dim PT_TopLeftCell As Range
    Set PT_TopLeftCell = PT.TableRange2.Cells(1, 1)

    '***Insert new temporary sheet
    Set WKS = ThisWorkbook.Worksheets.Add
    
    '***Copy Pivot table
    PT.TableRange2.Copy
    
    '***Insert Values
    WKS.Range("A1").PasteSpecial xlPasteValues
    
    '***Copy Pivot table
    PT.TableRange2.Copy
    
    '***Insert Formats
    WKS.Range("A1").PasteSpecial xlPasteFormats
    
    '***Copy temp. copy of Pivot table and paste to original Pivot table location
    WKS.Range("A1").CurrentRegion.Copy PT_TopLeftCell
    
    '***Delete temporary Worksheet
    Application.DisplayAlerts = False
    WKS.Delete
    Application.DisplayAlerts = True
    
    Set PT_TopLeftCell = Nothing
    Set PT = Nothing
    Set WKS = Nothing
End Sub
 
Upvote 0
Thanks for trying, but it doesn't work. It runs to the line:

WKS.Range("A1").CurrentRegion.Copy PT_TopLeftCell

And then delivers a run time error 1004.

I've done a ton of researching on this, and it appears that any copy/paste formats done against a pivot table in Excel 2007 doesn't work with VBA code, at least none that I can find. I've gotten around the issue by using an autoformat on the pasted values.

Thanks for trying.
 
Upvote 0
This code should solve your problem. :biggrin:

Code:
Sub CopyPivotValuesAndFormats()
    Dim WKS As Worksheet
    Dim PT As PivotTable
    Set PT = [COLOR=Red]ActiveCell[/COLOR].PivotTable
    Dim PT_TopLeftCell As Range
    Set PT_TopLeftCell = PT.TableRange2.Cells(1, 1)

    '***Insert new temporary sheet
    Set WKS = ThisWorkbook.Worksheets.Add
    
    '***Copy Pivot table
    PT.TableRange2.Copy
    
    '***Insert Values
    WKS.Range("A1").PasteSpecial xlPasteValues
    
    '***Copy Pivot table
    PT.TableRange2.Copy
    
    '***Insert Formats
    WKS.Range("A1").PasteSpecial xlPasteFormats
    
    '***Copy temp. copy of Pivot table and paste to original Pivot table location
    WKS.Range("A1").CurrentRegion.Copy PT_TopLeftCell
    
    '***Delete temporary Worksheet
    Application.DisplayAlerts = False
    WKS.Delete
    Application.DisplayAlerts = True
    
    Set PT_TopLeftCell = Nothing
    Set PT = Nothing
    Set WKS = Nothing
End Sub

See above (red)
Change the script as shown, select a cell within the pivottable and run the macro.
I assumed you have your pivot in Cell A1.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,864
Members
449,052
Latest member
Fuddy_Duddy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top