I tried to create pivot table on existing sheets but hitting run time error 13 (type mismatch) . I have changed data and pivot range to reference by address but still not workable. Please refer to my code as below:
VBA Code:
[/
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim Drange As Range
Dim Prange As Range
Dim LastRow As Long
Dim LastCol As Long
'Define Data Range
Set DSheet = ThisWorkbook.Worksheets("My")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set Drange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set Prange = DSheet.Cells(1, 13)
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:="'" & DSheet.Name & "'!" & Drange.Address(ReferenceStyle:=xlR1C1)). _
CreatePivotTable(TableDestination:="'" & DSheet.Name & "'!" & Prange.Address(ReferenceStyle:=xlR1C1), _
TableName:="MyPivotTable")
]