Tuesday 16 December 2014

Microsfot Excel - Macro - Simple example - How to create Pivot Table

Watch this example on YouTube

Example uses macro recorder

Here is my data (column A,B,C)

NameMonth Salary
FrankJan20
FrankFeb22
FrankMar33
BobJan30
BobFeb35
BobMar35

and here is my simple macro:

 Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A1:C7").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R7C3", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Sheet5!R3C1", TableName:="PivotTable2", DefaultVersion _
        :=xlPivotTableVersion14
    Sheets("Sheet5").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
        "PivotTable2").PivotFields("Salary"), "Sum of Salary", xlSum
End Sub




























No comments:

Post a Comment