Auto Create Pivot Table & Chart, Auto Refresh Pivot Table

 Auto Create Pivot Table

Option Explicit

Sub CreatePivotCache_N_PivotTable()

ThisWorkbook.Activate

Dim pc As PivotCache

Dim pt As PivotTable


Set pc = ThisWorkbook.PivotCaches.Create( _

SourceType:=xlDatabase, _

SourceData:=Sheet1.Range("A1").CurrentRegion, _

Version:=8)


Set pt = pc.CreatePivotTable(TableDestination:=Sheet4.Range("A5"), _

TableName:="MyFirstPivotTable")


'Add Pivot Fields

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("State").Orientation = xlRowField

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("Region").Orientation = xlRowField

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("Region").Position = 1

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("InsuredValue").Orientation = xlDataField

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("Sum of InsuredValue").Function = xlSum

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("Sum of InsuredValue").NumberFormat = "#,##0"

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("Location").Orientation = xlPageField

Sheet4.PivotTables("MyFirstPivotTable").PivotFields("Location").CurrentPage = "Urban"


Set pc = Nothing

Set pt = Nothing

End Sub


 Auto Refresh Pivot Table

Paste below CODE in the "Source Data Sheet Code Module".

Option Explicit


Private Sub Worksheet_Deactivate()

'Sheet4.PivotTables("MyFirstPivotTable").RefreshTable

Dim lng_LastRow As Long

Dim int_LastCol As Integer

Dim new_rng As Range


'Find Last Row & Last Col of Source Data

lng_LastRow = Cells.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

int_LastCol = Cells.Find(what:="*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column

Set new_rng = Sheet1.Range("A1", Cells(lng_LastRow, int_LastCol))

'Change Pivot Cache whenever Source Data Updates

Sheet4.PivotTables("MyFirstPivotTable").ChangePivotCache _

ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=new_rng, Version:=8)

End Sub


Auto Create Pivot Chart

Option Explicit

Dim shp As Shape

Sub AutoCreatePivotChart()

Call AutoDeleteShapes

Dim rng_SourceData As Range

Set rng_SourceData = Sheet4.Range("A5").CurrentRegion

Set shp = Sheet5.Shapes.AddChart2

shp.Chart.SetSourceData rng_SourceData

shp.Chart.ChartType = xlBarClustered

shp.Chart.ShowAllFieldButtons = False

shp.Chart.SetElement (msoElementDataLabelOutSideEnd)

shp.Chart.Axes(xlValue).Delete

shp.Chart.Axes(xlValue).MajorGridlines.Delete

'shp.Chart.HasTitle = True

shp.Chart.ChartTitle.Text = "Pivot Chart"

'Set the position of the Pivot Chart

shp.Left = Sheet5.Range("B3").Left

shp.Top = Sheet5.Range("B3").Top

'Set the height and width of the Pivot Chart

shp.Height = 300

shp.Width = 375

End Sub


Sub AutoDeleteShapes()

If Sheet5.Shapes.Count > 0 Then

    For Each shp In Sheet5.Shapes

        shp.Delete

    Next shp

End If

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