Excel – VBA script for multiple pivot tables

excelpivot tablevba

I replace source data ever week, and have many pivot tables attached to it. However, when I refresh the tables, they start to miss some data. So, I went ahead and recorded some "macros" to create and replace new pivot tables every time. The pasted areas will stay the same, and I gave them enough surrounding room to never overlap. Unfortunately, I get many errors and am not good with VBA at all. I only know it from forums etc.

The source data is on "CopyData"
The Country Pivot Tables should go on "Country"

So my questions are:
1. How can I create a VBA script that creates and replaces pivot tables and can be used many time after (So maybe start with deleting old pivots?)
2. How can I keep the pivot table's names general? I won't know what the next pivot table will be called when it is run

Any help would be great!

Below is my code for one of the "macros" and it should create and replace two pivot tables.

   Sub Pivots_Country()
' Pivots_Country Macro
'
'
    Range("B215").Select
    ActiveWorkbook.Worksheets("Region").PivotTables("PivotTable10").PivotCache. _
        CreatePivotTable TableDestination:="Sheet1!R2C2", TableName:="PivotTable10" _
        , DefaultVersion:=xlPivotTableVersion14
    Sheets("Sheet1").Select
    Cells(2, 2).Select
    ActiveSheet.PivotTables("PivotTable11").AddDataField ActiveSheet.PivotTables( _
        "PivotTable11").PivotFields("storeId"), "Count of storeId", xlCount
    With ActiveSheet.PivotTables("PivotTable11").PivotFields("NewCountry")
        .Orientation = xlRowField
        .Position = 1
    End With
    Sheets("CopyData").Select
    Range("B10").Select
    ActiveWorkbook.Worksheets("Sheet1").PivotTables("PivotTable11").PivotCache. _
        CreatePivotTable TableDestination:="Sheet1!R4C6", TableName:="PivotTable12" _
        , DefaultVersion:=xlPivotTableVersion14
    Sheets("Sheet1").Select
    Cells(4, 6).Select
    With ActiveSheet.PivotTables("PivotTable12").PivotFields("plan")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable12").PivotFields("planCode")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable12").PivotFields("NewCountry")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable12").AddDataField ActiveSheet.PivotTables( _
        "PivotTable12").PivotFields("storeId"), "Count of storeId", xlCount
    ActiveWindow.SmallScroll Down:=-7
    Range("B2:C56").Select
    Selection.Copy
    Sheets("Country").Select
    Range("B4").Select
    ActiveSheet.Paste
    ActiveWindow.SmallScroll Down:=7
    ActiveWindow.SmallScroll ToRight:=-2
    ActiveWindow.SmallScroll Down:=-2
    ActiveWindow.SmallScroll ToRight:=-1
    ActiveWindow.SmallScroll Down:=-1
    ActiveWindow.SmallScroll ToRight:=-2
    ActiveWindow.SmallScroll Down:=-30
    Sheets("Sheet1").Select
    ExecuteExcel4Macro "(""PivotTable12"",""planCode"",3,FALSE,TRUE)"
    Range("F2:P52").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Country").Select
    ActiveWindow.SmallScroll ToRight:=3
    ActiveWindow.SmallScroll Down:=-1
    ActiveWindow.SmallScroll ToRight:=2
    Range("R3").Select
    ActiveSheet.Paste
    ActiveWindow.SmallScroll Down:=1
    Range("Q15").Select
    Sheets("Sheet1").Select
    Cells.Select
    Range("F2").Activate
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    Range("J11").Select
    Sheets("CopyData").Select
End Sub

Best Answer

You really don't need to recreate the pivot tables. You need to find out why data is missing from the pivot tables after you refresh your data. This shoudl be a LOT less work.

I suspect the pivot tables are getting data from a range and that when you refresh the data the new data has more rows than the old data, so the pivot tables miss the extra rows.

The best way around this to ensure that you pivot tables are based on a range that has enough rows for all the current and future data you put into the sheet.

Inspect the pivot table.

  1. click on pivot table

  2. in ribbon>Pivot table tools>analyse>data>Change data source you can see what the range is (and change it)

Please let me know how you get on.

In the future you might consider using excel tables (also known as ListObjects)

For example

  1. select the range that has your data

  2. click the ribbon>Insert>Table button to convert the range to be an excel table.

  3. change your pivot tables to reference data in the tables

  4. Change how you refresh you data.

As you are new to excel, I wouldn't use Excel tables now, get it working first, and then convert it to use them.

I hope this helps. Harvey

Related Topic