从特定列数据读取的VBA代码

时间:2019-03-07 17:36:25

标签: excel vba

此VBA代码自动创建PT和图表。

我希望从“总计”列中读取图表。它是默认值,并从第一列“已关闭”中读取。

以下是我的代码:

Sub PT_C_RiskType_Status_test()
'
' PT_C_RiskType_Status_test Macro
'


    'Defining the variables

    Dim pc As PivotCache
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim sh As Shape
    Dim ch As Chart


    Set pc = ThisWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:="DataSource", _
    Version:=xlPivotTableVersion15)


    Set ws = Sheets.Add

    ws.Select
    Cells(3, 1).Select
    Range("A3:D9").Select


     'Create the pivot table

    Set pt = pc.CreatePivotTable(TableDestination:=ws.Name & "!R3C1", _
                                TableName:="Pivottable")


     'Adding the data fields

    pt.AddDataField _
        Field:=pt.PivotFields("Status"), _
        Function:=XlConsolidationFunction.xlCount

     'Adding the fields


        pt.AddFields _
        RowFields:="Risk Type", _
        ColumnFields:="Status"


    'Adding chart

    Set sh = ws.Shapes.AddChart2(XlChartType:=XlChartType.xlPie, _
                Width:=200, Height:=200)


    Set ch = sh.Chart

End Sub

请告知。

0 个答案:

没有答案