I've searched and found Create Pivot tables within a for loop, but haven't been able to get it to work.
I've got a workbook with 11 sheets and a table in each sheet, so I'd like to create a loop that will go through every sheet and create a pivot table next to the existing table.
I'm getting an "Invalid Procedure or argument " error for the Pivot Table code.
This code is probably far too simple, but here's my attempt:
sub PivotLoop()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Set cache = Nothing
Set PivT = Nothing
With ws
On Error Resume Next
On Error GoTo 0
Set cache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=Range("Table" & ws.Index), Version:=xlPivotTableVersion15)
Set PivT = ws.PivotTables.Add(PivotCache:=cache, _
TableDestination:=Range("h2"), TableName:="PivotTable" & ws.Index)
End With
Next ws
End Sub