I'm trying to integrate the creation of a power pivot and pivot chart in VBA and have worked out a big part. However, I am encountering a problem which I cannot seem to overcome
I create new worksheets where i create the Pivot and export it to PDF. when this is done, i delete the pivottable and worksheet with:
Sub DeleteAllPivotTables()
PURPOSE: Delete all other sheets and pivots
Dim sht As Worksheet
Dim pvt As PivotTable
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sht In ActiveWorkbook.Worksheets
If sht.Name <> "Output" Then
If sht.Name <> "Syntax" Then
For Each pvt In sht.PivotTables
pvt.TableRange2.Clear
Next pvt
sht.Delete
End If
End If
Next sht
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
When I delete the sheet without the part of the macro for deleting the pivot tables, I encounter a run-time 1004 error. In my knowledge because the pivotname "PivotTable1" is already taken and not deleted with the macro. When the sheet is already deleted, how can I solve this issue?
Thanks in advance!