how to Create pivot table in new worksheet. i am using below code but getting error

Apurva

New Member
Dim rng as Object
Dim rng1 as Object

Dim wb as Object = GetWorkbook(handle,workbookname)
Dim ws as Object = GetWorksheet(handle,workbookname,worksheetname,createifmissing)

sheetexists = ws IsNot Nothing

If sheetexists then ws.Activate()

rng = ws.UsedRange()
rng1=ws.newSheet.Range("A1")
Dim PCache as object = wb.PivotCaches.Create(SourceType:=1, SourceData:= rng).CreatePivotTable _
(TableDestination:=rng1, TableName:="PivotTable1")


With ws.PivotTables("PivotTable1").PivotFields("HWRN")
.Orientation = 1
.Position = 1
End With

With ws.PivotTables("PivotTable1").PivotFields("Productline")
.Orientation = 2
.Position = 1
End With

With ws.PivotTables("PivotTable1").PivotFields("Quantity")
.Orientation = 4
.Position = 1
End with
 
Top