How to create multiple pivot tables with their own charts on the same sheet using a macro?

I am new to VBA so I apologize if this is a rookie mistake. I am attempting to create several pivot tables on the same sheet with each one having their own chart. The goal is to do this every month as the data will be different monthly and the resulting pivot tables will have a dynamic range. I can create each pivot table and have their filters applied correctly with no issue. However, when I create the charts for each table, I run into errors defining the source data. The exact error is “Run-time error ‘-2147467259 (80004005)’: Method ‘SetSourceData’ of object ‘_Chart’ failed”.

The dummy data I’m using is just 2 columns with 1’s and 0’s each titled “Missing Form1” and “Missing Form2”. The source data will be the same for each pivot table. Here is the macro I wrote:

Sub Macro2()

    Dim pcLinkage As PivotCache
    Dim PivotTableName As PivotTable
    Dim PivotTableName2 As PivotTable
    Dim wsNew As Worksheet
    Dim Form1Chart As ChartObject
    Dim Form2Chart As ChartObject
    
    ' Create pivot cache
    Set pcLinkage = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:=ActiveWorkbook.Worksheets("Link").Range("A1").CurrentRegion.Address)
    
    ' Add a new worksheet
    Set wsNew = Sheets.Add
    
    ' Create first pivot table
    Set PivotTableName = pcLinkage.CreatePivotTable( _
        TableDestination:=wsNew.Name & "!R1C1", TableName:= _
        "PivotTableName")
    
    ' Add data field to first pivot table
    With PivotTableName
        .AddDataField .PivotFields("Missing Form1"), _
            "Count of Missing Form1", xlCount
    End With
    
    ' Create chart for first pivot table
    Set Form1Chart = wsNew.ChartObjects.Add(Left:=300, Top:=200, Width:=550, Height:=200)
    With Form1Chart.Chart
'This works exactly as intended. In my live workbook, the filter methods and formatting are applied correctly'
        .SetSourceData Source:=PivotTableName.TableRange1
        .ChartWizard Source:=PivotTableName.TableRange1, _
            Gallery:=xlColumn, Title:="Missing Form1 Turnbacks", HasLegend:=False
    End With
    
    ' Create second pivot table
    Set PivotTableName2 = pcLinkage.CreatePivotTable( _
        TableDestination:=wsNew.Name & "!R1C4", TableName:= _
        "PivotTableName2")
    
    ' Add data field to second pivot table
    With PivotTableName2
        .AddDataField .PivotFields("Missing Form2"), _
            "Count of Missing Form2", xlCount
    End With
    
    ' Create chart for second pivot table
    Set Form2Chart = wsNew.ChartObjects.Add(Left:=300, Top:=500, Width:=550, Height:=200)
    With Form2Chart.Chart
'This is where the error occurs'
        .SetSourceData Source:=PivotTableName2.TableRange1
'I tried to use this .ChartWizard by itself but it creates a chart using the data from PivotTableName'
        .ChartWizard Source:=PivotTableName2.TableRange1, _
            Gallery:=xlColumn, Title:="Missing Form2", HasLegend:=False
    End With

End Sub

I have tried making this into a Shape and explicitly defining the range of the pivot table but run into the same error. I have tried using the .ChartWizard method, but it makes each chart connected and use PivotTableName data (same filters, same data series) instead of PivotTableName2, etc. I have also tried to create a new pivot cache for the second pivot table but I ran into the same issues.

I’m at a loss here I can’t figure out what I’m doing wrong when looking at the method libraries online or related articles here. Any help is appreciated!

New contributor

Cheesemaster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật