VBA Code for copying and pasting data from pivot to another excel

Monicasinha

Board Regular
Joined
Dec 26, 2022
Messages
51
Office Version
  1. 365
Platform
  1. Windows
Hi

Can you please suggest a better vba code for copying and pasting data from a pivot to another excel.

The code below is taking around 20 seconds. Can something be done so that it takes lesser time.
----------------------------------------------------------------------------------------------------------------------------------

Set PTable = Openbook.Worksheets("PivotTable").PivotTables("Pivot1")

PTable.PivotSelect "'Activity'[All]", xlLabelOnly, True

Selection.Copy

ThisWorkbook.Worksheets("FAB Solution").Range("E5").PasteSpecial xlPasteValues


PTable.PivotSelect "'Country/Location'[All]", xlLabelOnly, True

Selection.Copy

ThisWorkbook.Worksheets("FAB Solution").Range("L5").PasteSpecial xlPasteValues


PTable.PivotSelect "'Career Level'[All]", xlLabelOnly, True

Selection.Copy

ThisWorkbook.Worksheets("FAB Solution").Range("M5").PasteSpecial xlPasteValues


PTable.PivotSelect "'Service Group'[All]", xlLabelOnly, True

Selection.Copy

ThisWorkbook.Worksheets("FAB Solution").Range("N5").PasteSpecial xlPasteValues



PTable.PivotSelect "'Billable Hours' Resource", xlDataOnly, True

Selection.Copy

ThisWorkbook.Worksheets("FAB Solution").Range("G5").PasteSpecial xlPasteValues



PTable.PivotSelect "'Revenue Recognition' Resource", xlDataOnly, True

Selection.Copy

ThisWorkbook.Worksheets("FAB Solution").Range("H5").PasteSpecial xlPasteValues
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hello,

Yes, there are a few ways to optimize the code to make it run faster and more efficiently. One method is to disable certain Excel features, such as screen updating and automatic calculation, while the code is running.

Another approach is to minimize the number of times you interact with the worksheet by consolidating some of the operations.

Here's an optimized version of the code:

VBA Code:
Sub CopyPivotTableData()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim PTable As PivotTable
    Set PTable = ThisWorkbook.Worksheets("PivotTable").PivotTables("Pivot1")

    Dim wsDestination As Worksheet
    Set wsDestination = ThisWorkbook.Worksheets("FAB Solution")

    Dim arrFieldNames() As Variant
    arrFieldNames = Array("Activity", "Country/Location", "Career Level", "Service Group")

    Dim arrDestRanges() As Variant
    arrDestRanges = Array("E5", "L5", "M5", "N5")

    Dim i As Long
    For i = LBound(arrFieldNames) To UBound(arrFieldNames)
        PTable.PivotSelect "'" & arrFieldNames(i) & "'[All]", xlLabelOnly, True
        Selection.Copy
        wsDestination.Range(arrDestRanges(i)).PasteSpecial xlPasteValues
    Next i

    PTable.PivotSelect "'Billable Hours' Resource", xlDataOnly, True
    Selection.Copy
    wsDestination.Range("G5").PasteSpecial xlPasteValues

    PTable.PivotSelect "'Revenue Recognition' Resource", xlDataOnly, True
    Selection.Copy
    wsDestination.Range("H5").PasteSpecial xlPasteValues

    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub


Hope this helps you. Good luck!

Chris
 
Upvote 0
Solution

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

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