【问题标题】:VBA: Create multiple pivot charts from multiple pivot tables in 1 sheet [Error: Method 'SetSourceData' of object '_chart' failed]VBA:从一张表中的多个数据透视表创建多个数据透视图[错误:对象'_chart'的方法'SetSourceData'失败]
【发布时间】:2014-09-29 19:46:54
【问题描述】:

首先,我在 1 个名为 OOTWeeklyTrendperPlatform、OOTWeeklyTrendperFailureMode、OverallWeeklyTrendperDereel、OverallWeeklyTrendperEngDataValue 的工作表中创建了 4 个数据透视表。这些部分已成功完成。

在这 4 个数据透视表之后,我想创建 数据透视图。因此,1 个工作表中有 4 个数据透视表和 4 个数据透视图。

我的工作顺序是这样的(表格和图表名称用于说明目的)table1 然后是 chart1 然后是 table2 然后是 chart2 等等。问题是完成table1、chart1、table2之后;生成chart2(或第二个图表对应于table2)时出现错误。请在下面找到缩短的代码。

Sub MakePivotTableDereel()
    Dim PTCache As PivotCache, PTCache1 As PivotCache
    Dim PT As PivotTable, PT1 As PivotTable, PT2 As PivotTable, PT3 As PivotTable

    Dim rngChart As Range, rngChart1 As Range, rngChart2 As Range, rngChart3 As Range
    Dim objChart As ChartObject, objChart1 As ChartObject, objChart2 As ChartObject, objChart3 As ChartObject

    Dim PivotDereel As Worksheet
    Application.ScreenUpdating = False

'   Delete PivotSheet if it exists
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("PivotDereel").Delete
    On Error GoTo 0

'   Create Pivot Cache
    Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("Data").Range("A1").CurrentRegion.Address)

'   Add PivotDereel sheet
    Set PivotDereel = Worksheets.Add
    ActiveSheet.Name = "PivotDereel"
    Cells(1, 1).Value = "OOT Weekly Trend per Platform"
    Cells(1, 1).Font.Size = 16
    Cells(1, 15).Value = "OOT Weekly Trend per Failure Mode"
    Cells(1, 15).Font.Size = 16
    Cells(1, 29).Value = "Overall Weekly Trend per Dereel"
    Cells(1, 29).Font.Size = 16
    Cells(1, 39).Value = "Overall Weekly Trend per Eng Data Value"
    Cells(1, 39).Font.Size = 16

'   Create pivot table OOTWeeklyTrendperPlatform
    Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, TableDestination:=PivotDereel.Cells(4, 1), TableName:="OOTWeeklyTrendperPlatform")
    ' set table properties below
    With PT
        .PivotFields("Work Week").Orientation = xlRowField
        .PivotFields("PLATFORM").Orientation = xlColumnField
        .PivotFields("EngDataValue").Orientation = xlPageField
        .PivotFields("LotID").Orientation = xlDataField
        .DisplayFieldCaptions = False
        .TableStyle2 = "PivotStyleMedium2"
        .PivotFields("Count of LotID").Caption = "Lot ID"
    End With
    ' Filter unwanted data below
    PT.PivotFields("EngDataValue").CurrentPage = "(All)"
    With PT.PivotFields("EngDataValue")
        .PivotItems("BROKEN SEAL").Visible = False
        '.PivotItems("DAMAGED MATERIAL").Visible = False
        .PivotItems("COGHOLE DAMAGE").Visible = False
        '.PivotItems("DEBRIS IN TAPE").Visible = False
        .PivotItems("DRIFTED COVER TAPE").Visible = False
        '.PivotItems("DROPPED REEL").Visible = False
        .PivotItems("EMPTY POCKET").Visible = False
        '.PivotItems("ENGINEERING REQUEST").Visible = False
        .PivotItems("LOOSE COVER TAPE").Visible = False
        .PivotItems("MACHINE COUNT ERROR").Visible = False
        .PivotItems("MACHINE LOCKED UP").Visible = False
        .PivotItems("OUT OF MATERIALS").Visible = False
        .PivotItems("OUT OF PURGE TIME").Visible = False
        '.PivotItems("OVER/UNDER SEALED").Visible = False
        .PivotItems("PART OUT OF POCKET").Visible = False
        '.PivotItems("PO ERROR").Visible = False
        .PivotItems("TECH PURGED SYSTEM").Visible = False
        .PivotItems("THICK/THIN SEAL").Visible = False
    End With
    PT.PivotFields("EngDataValue").EnableMultiplePageItems = True

'   Create pivot chart OOTWeeklyTrendperPlatform
    Debug.Print PT.TableRange2.Columns.Count
    'Use the ChartObjects.Add Method to add an embedded Pivot Chart, which is represented as a ChartObject object. Note that the arguments Left and Width are mandatory to specify in this method. This method allows you to set the position and size (both in points) of the chart.
    Set objChart = Sheets("PivotDereel").ChartObjects.Add(Left:=10, Top:=25 * (PT.TableRange2.Rows.Count), Width:=300, Height:=200)
    'set data source range for the Chart:
    Set rngChart = PT.TableRange2
    With objChart.Chart
    .SetSourceData Source:=rngChart
    End With

    With objChart.Chart
        .HasTitle = True
        .ChartTitle.Characters.Text = "OOT Weekly Trend per Platform"
        .ChartTitle.Font.Bold = True
        .ChartTitle.Font.Size = 14
    End With

'   Create pivot table OOTWeeklyTrendperFailureMode
    Set PT1 = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, TableDestination:=PivotDereel.Cells(4, 15), TableName:="OOTWeeklyTrendperFailureMode")
    With PT1
        .PivotFields("Work Week").Orientation = xlRowField
        .PivotFields("Failure Mode").Orientation = xlColumnField
        .PivotFields("EngDataValue").Orientation = xlPageField
        .PivotFields("LotID").Orientation = xlDataField
        .DisplayFieldCaptions = False
        .TableStyle2 = "PivotStyleMedium2"
        .PivotFields("Count of LotID").Caption = "Lot ID"
    End With
    PT1.PivotFields("EngDataValue").CurrentPage = "(All)"
    With PT1.PivotFields("EngDataValue")
        .PivotItems("BROKEN SEAL").Visible = False
        '.PivotItems("DAMAGED MATERIAL").Visible = False
        .PivotItems("COGHOLE DAMAGE").Visible = False
        '.PivotItems("DEBRIS IN TAPE").Visible = False
        .PivotItems("DRIFTED COVER TAPE").Visible = False
        '.PivotItems("DROPPED REEL").Visible = False
        .PivotItems("EMPTY POCKET").Visible = False
        '.PivotItems("ENGINEERING REQUEST").Visible = False
        .PivotItems("LOOSE COVER TAPE").Visible = False
        .PivotItems("MACHINE COUNT ERROR").Visible = False
        .PivotItems("MACHINE LOCKED UP").Visible = False
        .PivotItems("OUT OF MATERIALS").Visible = False
        .PivotItems("OUT OF PURGE TIME").Visible = False
        '.PivotItems("OVER/UNDER SEALED").Visible = False
        .PivotItems("PART OUT OF POCKET").Visible = False
        '.PivotItems("PO ERROR").Visible = False
        .PivotItems("TECH PURGED SYSTEM").Visible = False
        .PivotItems("THICK/THIN SEAL").Visible = False
    End With
    PT1.PivotFields("EngDataValue").EnableMultiplePageItems = True

'   Create pivot chart OOTWeeklyTrendperFailureMode

    'Use the ChartObjects.Add Method to add an embedded Pivot Chart, which is represented as a ChartObject object. Note that the arguments Left and Width are mandatory to specify in this method. This method allows you to set the position and size (both in points) of the chart.
    Set objChart1 = Sheets("PivotDereel").ChartObjects.Add(Left:=10, Top:=25 * (PT1.TableRange2.Rows.Count), Width:=300, Height:=200)
    'set data source range for the Chart:
    Set rngChart1 = PT1.TableRange2

    With objChart1.Chart
    .SetSourceData Source:=rngChart1
    End With

    With objChart.Chart
        .HasTitle = True
        .ChartTitle.Characters.Text = "OOT Weekly Trend per Platform"
        .ChartTitle.Font.Bold = True
        .ChartTitle.Font.Size = 14
    End With
'below is the portion to create 3rd and 4th pivot tables and charts
end sub

我收到错误:对象“_chart”的方法“SetSourceData”失败部分,

With objChart1.Chart
        .SetSourceData Source:=rngChart1
        End With

这部分假设为第二个图表分配源数据。但它失败了。你有解决办法吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果选择了数据透视表中的任何单元格,您插入的图表将自动使用整个数据透视表作为其源数据。我只是在一个带有一些数据透视表的工作表上运行它:

    for i=1 to 4
      activesheet.pivottables(i).tablerange2.cells(1,1).select
      ActiveSheet.Shapes.AddChart
      ' here you need to specify chart type and style, size and position, etc.
    next
    

    不需要 SetSourceData(在我按上述简化之前,这给了我意想不到的问题)。

    【讨论】:

    • 由于我现在在外面,还没有尝试过您的解决方案。但我对 AddChart2 332 部分很好奇。 332指的是什么?
    • 322 是图表类型。
    • 在什么情况下不起作用?有错误信息吗?看起来有什么不对吗?
    • 我刚刚发现AddChart2是Excel 2013中新增的,具体的样式332可能也是这个版本的新增。我会更新我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多