VBA – Delete PivotTables

This code snippet shows how to loop through all pivot tables in all worksheets and delete them:


Sub DeletePivotTables()

    Dim Pt As PivotTable
    Dim Ws As Worksheet
    
'Loop through worksheets 
    For Each Ws In ActiveWorkbook.Worksheets
    Worksheets(Ws.Name).Select

'Loop through pivot tables         
    For Each Pt In Ws.PivotTables
           
'Delete pivot table
           Pt.PivotSelect "", xlDataAndLabel, True
    Selection.Delete Shift:=xlToLeft

            'Exit Sub 'Optional: Get out
        Next Pt
    Next Ws
End Sub  


Sunday, March 20th, 2011 Excel, VBA