• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help with VBA Code - Copying pivot table

k3vsmith

Member
Hello guys,
I need assistance with code.
I have a pivot table called "SIOPPivot" that I want to make a copy of and paste directly next to it. I want to add three additional rows and make some column adjustments. See my attachment. Worksheet 1 is what I have currently and worksheet 2 is what I want it to look like. Any assistance is greatly appreciated.

What I was thinking is to copy the SIOPPivot table down below then add the three rows. Then move the copy next to SIOPPivot and adjust columns there after? Not sure if thats best solution?
 

Attachments

  • CopyPvt.xlsx
    35 KB · Views: 2
You could do this:

Code:
Sub copyPivot()
    Dim pt                    As PivotTable
    Dim rgDest                As Range
    Dim lCount                As Long
   
    Set pt = ActiveSheet.PivotTables(1)

    With pt
        With .TableRange1
            .Resize(.Rows.Count - 1, .Columns.Count).Copy
            lCount = .Columns.Count
            Set rgDest = .Offset(, lCount + 1)
        End With
    End With
    With rgDest
        .PasteSpecial xlPasteAll
        .Rows(1).ClearContents
        .Rows(3).Resize(3).Insert shift:=xlDown
        .Rows(3).Resize(3).ClearFormats
        .Columns(lCount).ClearContents
        .Rows(2).Cells(1, lCount).Value = "Prior1"
    End With

End Sub
 
Back
Top