While running the below code "Type mismatch" error appears when the macro reaches this line:
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), TableName:="ERA_Dashboard")
How do I correct the error on the code?
Sub Pivot()
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Application.DisplayAlerts = False
Worksheets("Summary").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Summary"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Summary")
Set DSheet = Worksheets("Content")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
Sheets("Content").Select
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), TableName:="ERA_Dashboard")
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="ERA_Dashboard")
With ActiveSheet.PivotTables("ERA_Dashboard").PivotFields("Issue Status")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("ERA_Dashboard").PivotFields("Issue Status")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Issue Status"
End With
End Sub
There is no need to delete and re-create "Summary" sheet, just use the code below. The code below will create (or update) the
PTable
Pivot-Table in "Summary" worksheet with the updatedPCache
.Code
Edit 1: