I want to create a pivot table in a new workbook from data selected in another workbook.
I have tried the code from this answer but I get a reference error "1004" in the last line of code.
Sub test() Dim wb As Workbook Dim ws As Worksheet Dim pRange As Range Dim pc As PivotCache Dim pt As PivotTable Dim InitialPivotCache As PivotCache Dim CopyPivotCache As PivotCache Set wb = Workbooks.Add Set ws = wb.Worksheets(1) Set pRange = ThisWorkbook.Sheets(1).Range("A1:B6") Set pc = ThisWorkbook.PivotCaches.Create(xlDatabase, pRange) 'Cache in ThisWorkbook Call CountCaches Set CopyPivotCache = wb.PivotCaches.Create(pc.SourceType, pc.SourceData) Set pt = ws.PivotTables.Add(CopyPivotCache, Range("A3"), "MyPivotTable") End Sub I Counted number of caches with this code in my thisworkbook after I have set the "pc" cache I found 0 caches.
Sub CountCaches() MsgBox "There are " _ & ThisWorkbook.PivotCaches.Count _ & " pivot caches in the active workook." End Sub I don't know what's wrong in my code. How can I fix it?
ListObject) will do.