3
Sub pivottable()

Dim s As Worksheet
Dim pc As PivotCache
Dim Pt As pivottable
Dim data As Range


    Sheets("PivotTable").Delete


    Set s = Worksheets.Add(after:=Sheets("Grocery Input"))
    s.Name = "PivotTable"
    Set data = Groceries.Range("a1").CurrentRegion
    data.Name = "Food"
    Set s = Sheets("PivotTable")
    Set pc = ThisWorkbook.PivotCaches.Create(xlDatabase, "Food", xlPivotTableVersion12)
    Set Pt = pc.CreatePivotTable("'PivotTable'!r1c1", "PivotTable25", xlPivotTableVersion12)


    s.Select
    Cells(1, 1).Select

Pt.AddDataField s.PivotTables( _
        "PivotTable1").PivotFields("Total Price:"), "Sum of Total Price:", xlSum

    With Pt.PivotFields("Category:")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With Pt.PivotFields("Date:")
        .Orientation = xlRowField
        .Position = 1
    End With
    With Pt.PivotFields("Sum of Total Price:")
        .Calculation = xlPercentOfRow
        .NumberFormat = "0.00%"
    End With

Pt.AddDataField s.PivotTables("PivotTable1").PivotFields("Total Price:"), "Sum of Total Price:2", xlSum

End Sub
4

0 回答 0