I tried to create pivot table on existing sheet using VBA but it hit run time error '13' . Type mismatch.
Here is the code I'm using.
Here is the code I'm using.
VBA Code:
[/
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PRange As Range
Dim Destination As Range
Set DSheet = Worksheets("1")
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)
Set Destination = Worksheets(1).Range("H1")
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=Destination, _
TableName:="PivotTable")
]