Create Pivot Table and Pivot Chart Using VBA and Auto Refresh the Pivot Table Whenever the Source Data Changes

 Pivot Table - Assuming the Data and the Pivot table both are on Sheet1 in the workbook.

Sub PivotTableChart()

Dim pc As PivotCache

Dim pt As PivotTable

Dim lrow As Integer

Dim lcol As Integer

Dim rng As Range

ThisWorkbook.Activate

Sheet1.Activate

lrow = Cells(Rows.Count, 1).End(xlUp).Row

lcol = Cells(1, Columns.Count).End(xlToLeft).Column

'Debug.Print lrow & "-" & lcol

Set rng = Range(Cells(1, 1), Cells(lrow, lcol))

'Debug.Print rng.Address


Set pc = ThisWorkbook.PivotCaches.Create(xlDatabase, rng, 6)

Set pt = pc.CreatePivotTable(Sheet1.Range("z5"), "MyPivot")

With Sheet1.PivotTables("MyPivot")

    With .PivotFields("Location")

            .Orientation = xlRowField

            .Position = 1

    End With

    

'    .AddDataField Sheet1.PivotTables("MyPivot").PivotFields("InsuredValue"), _

'    "Sum of InsuredValue", xlSum

    With .PivotFields("InsuredValue")

            .Orientation = xlDataField

            .Position = 1

            .Function = xlSum

            '.fieldsname = "Sum of InsuredValue"

            .NumberFormat = "#,##0"

    End With

    Note:

    'If (.Function =xlSum ) not works then use below code

    'Ex: .PivotFields("Sum of InsuredValue").Function = xlSum


    With .PivotFields("Region")

            .Orientation = xlPageField

            .Position = 1

    End With

End With

    Call Pivot_Pie_Chart

'Sheet1.PivotTables("MyPivot").PivotFields("Region").ClearAllFilters

'    Sheet1.PivotTables("MyPivot").PivotFields("Region").CurrentPage = "East"

'Sheet1.Columns("AA").NumberFormat = "00,000.0"

Set pc = Nothing

End Sub

Pivot Chart: Assuming the Pivot table and the Pivot Chart both are on Sheet1 in the workbook.

Sub Pivot_Pie_Chart()

'Create Pie or any chart

Dim sh As Shape

Set sh = Sheet1.Shapes.AddChart2

'sh.Chart.SetDefaultChart xlPie

sh.Chart.SetSourceData Sheet1.Range("aa5")

sh.Chart.ChartType = xlPie

sh.Chart.HasTitle = True

sh.Chart.SetElement msoElementDataLabelOutSideEnd

sh.Chart.ChartTitle.Text = "Pie Chart"

sh.Chart.ShowAllFieldButtons = False

sh.Chart.Parent.Left = Range("ac5").Left

sh.Chart.Parent.Top = Range("ac5").Top

'sh.Chart.Parent.Width = 350

'sh.Chart.Parent.Height = 250

sh.Chart.SetElement msoElementLegendBottom

Sheet1.Range("a1").Select

End Sub


 Auto Refresh Pivot Table: Keep the below code in the sheet1 code module (Select Worksheet in Object and SelectionChange in Procedure) to auto-refresh the Pivot Table whenever source data changes.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

ThisWorkbook.Activate

Sheet1.Activate

lrow = Cells(Rows.Count, 1).End(xlUp).Row

lcol = Cells(1, Columns.Count).End(xlToLeft).Column

'Debug.Print lrow & "-" & lcol

Set rng = Range(Cells(1, 1), Cells(lrow, lcol))

'Debug.Print rng.Address


'Set pc = ThisWorkbook.PivotCaches.Create(xlDatabase, rng, 6)

'Set pt = pc.CreatePivotTable(Sheet1.Range("z5"), "MyPivot")

'Debug.Print rng.Address


PivotTables("MyPivot").ChangePivotCache ActiveWorkbook.PivotCaches. _

        Create(SourceType:=xlDatabase, SourceData:= _

        rng, Version:=7)

'ActiveWorkbook.RefreshAll

'ThisWorkbook.RefreshAll

End Sub


Comments

Popular posts from this blog

Power Automate - Automatically fetch data from Power BI in to Excel and Send the copy of the Excel file via Email

Separate Text (Characters) & Numbers from Alpha Numeric String Using Formula and Macro (VBA)

File System Object