U
ub
Hi
I am trying to create a pivot table using VBA.
I have a workbook in which sheet1 has data, I have a commondbutton on sheet2
and on click on this button , I am trying to create pivot table on sheet
PivotSheet
My PivotSheet gets created but with no pivot table data. Please advise what
am I doing wrong in this code
Dim ptcache As PivotCache
Dim pt As PivotTable
Application.ScreenUpdating = False
On Error Resume Next
Application.DisplayAlerts = False
Sheets("PivotSheet").Delete
On Error GoTo 0
Set ptcache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=Sheets("Sheet1").Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "PivotSheet"
Set pt = ptcache.CreatePivotTable( _
TableDestination:=Sheets("PivotSheet").Range("A1"), _
TableName:="File")
With pt
.PivotFields(3).Orientation = x1RowField
.PivotFields(15).Orientation = x1ColumnField
.PivotFields(5).Orientation = x1DataField
End With
I am trying to create a pivot table using VBA.
I have a workbook in which sheet1 has data, I have a commondbutton on sheet2
and on click on this button , I am trying to create pivot table on sheet
PivotSheet
My PivotSheet gets created but with no pivot table data. Please advise what
am I doing wrong in this code
Dim ptcache As PivotCache
Dim pt As PivotTable
Application.ScreenUpdating = False
On Error Resume Next
Application.DisplayAlerts = False
Sheets("PivotSheet").Delete
On Error GoTo 0
Set ptcache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=Sheets("Sheet1").Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "PivotSheet"
Set pt = ptcache.CreatePivotTable( _
TableDestination:=Sheets("PivotSheet").Range("A1"), _
TableName:="File")
With pt
.PivotFields(3).Orientation = x1RowField
.PivotFields(15).Orientation = x1ColumnField
.PivotFields(5).Orientation = x1DataField
End With