Pivot table using VBA

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top