Tuesday, December 1, 2015

How to delete all Pivot tables in active workbook using VBA code?

Pivot table is a very useful and powerful functionality for summarizing or calculating data in Excel, So we may usually insert Pivot tables into a worksheet or multiple worksheets.But, Sometimes we need to delete all pivot tables from active workbook to create brand new  pivot tables which Target new data and design. It will be a very time consuming task to delete all pivot table from workbook which contains large number of pivot table created.

Do you know how to delete all pivot tables in the whole workbook?



Here is the code which is very useful to delete all pivot tables from active workbook.

Sub Delete_All_PivotTables()

' Author: Dreams24
' Written for VBA Tricks and tips blog
https://vbatricksntips.com

Dim pt As PivotTable

Dim ws As Worksheet
    
'For loop through each worksheets
    For Each ws In ActiveWorkbook.Worksheets
        Worksheets(ws.Name).Select

'Loop to select each pivot table for a single worksheet

        For Each pt In ws.PivotTables
           
'Delete a selected pivot table on worksheet
            pt.PivotSelect "", xlDataAndLabel, True
            Selection.Delete Shift:=xlToLeft
            
            'Exit Sub 'Optional: Get out
        Next pt
    Next ws
End Sub

No comments: