Copy and paste pivot table results using a macro

Anonymous
2017-12-29T15:20:25+00:00

This a follow up to a previous question. I have recorded a macro that is supposed to swap out pivot table fields and along the way copy and paste values from one cell to another e.g. copy from cell D22 and paste in F1, copy from cell D22 and paste in F2, and so on. While it looks fine during record, in practice it leaves #DIV/0! instead of the copy and pasted value. Provided is a screenshot and the code.

Sub Run_Pivot()

'

' Run_Pivot Macro

'

'

    Range("A1").Select

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Question 11")

        .Orientation = xlRowField

        .Position = 1

    End With

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Answer 11")

        .Orientation = xlRowField

        .Position = 2

    End With

    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _

        "PivotTable1").PivotFields("Answer 11"), "Count of Answer 11", xlCount

    Range("D22").Select

    Selection.Copy

    Range("F1").Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

        :=False, Transpose:=False

    Range("A1").Select

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Answer 11"). _

        Orientation = xlHidden

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Answer 11").Orientation = _

        xlHidden

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Question 11").Orientation _

        = xlHidden

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Question 12")

        .Orientation = xlRowField

        .Position = 1

    End With

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Answer 12")

        .Orientation = xlRowField

        .Position = 2

    End With

    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _

        "PivotTable1").PivotFields("Answer 12"), "Count of Answer 12", xlCount

    Range("D22").Select

    Application.CutCopyMode = False

    Selection.Copy

    Range("F2").Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

        :=False, Transpose:=False

    Range("A1").Select

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Answer 12"). _

        Orientation = xlHidden

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Answer 12").Orientation = _

        xlHidden

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Question 12").Orientation _

        = xlHidden

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Question 13")

        .Orientation = xlRowField

        .Position = 1

    End With

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Answer 13")

        .Orientation = xlRowField

        .Position = 2

    End With

    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _

        "PivotTable1").PivotFields("Answer 13"), "Count of Answer 13", xlCount

    Range("C22").Select

    Application.CutCopyMode = False

    Selection.Copy

    Range("F3").Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

        :=False, Transpose:=False

    Range("A1").Select

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Answer 13"). _

        Orientation = xlHidden

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Answer 13").Orientation = _

        xlHidden

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Question 13").Orientation _

        = xlHidden

    Range("C1").Select

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Anonymous
    2017-12-29T15:48:00+00:00

    Based on your attached image, the code is copying cells C22 and D22, both of which are #DIV/0!, to cells F1, F2, and F3 - so it is working correctly in copying that value to those cells.

    0 comments No comments
  2. Anonymous
    2017-12-29T16:37:12+00:00

    The problem being as the macro runs swapping out fields in the pivot table there are values that appear and disappear. As they appear, they are suppose to be copied and pasted into the applicable destination cells. What you see in the screen shot is what it looks like after it runs.

    0 comments No comments
  3. Anonymous
    2017-12-29T22:33:50+00:00

    The way the code is set up is to always put whatever ends up as the last value in cells C22 and D22 into F1, F2, and F3. That's why the end result is the #DIV/0.

    0 comments No comments
  4. Anonymous
    2017-12-29T22:54:43+00:00

    How do I make retain the value that's pasted after the switch? Is it something in the VBA or within the app itself?

    0 comments No comments
  5. Anonymous
    2017-12-30T01:01:41+00:00

    Are you able to save the file to OneDrive and post the link here? I can then take a look.

    0 comments No comments