Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88066

Creating a Pivot Table in VBA, in Excel 2013

$
0
0

I wrote this code in VBA for creating a Pivot Table (Converted it from a Macro, acctually...), and it doesn't work

Dim wsTarget As Worksheet
Dim rngSource As Range
Dim pc As PivotCache
Dim pt As PivotTable
Dim field As PivotField

Application.ScreenUpdating = False

Set rngSource = Summary.Range("A1").CurrentRegion
Set wsTarget = PivotTable

wsTarget.Select
For Each pt In wsTarget.PivotTables
    pt.Delete
Next pt

Set pc = ThisWorkbook.PivotCaches.Create(xlDatabase, rngSource, xlPivotTableVersion15)
Set pt = pc.CreatePivotTable(wsTarget.Range("A3"), "PivotTable1", , xlPivotTableVersion15)

Set field = wsTarget.PivotTables("PivotTable1").PivotFields("A")
field.Orientation = xlColumnField
field.Position = 1
field.LayoutBlankLine = True

Set field = wsTarget.PivotTables("PivotTable1").PivotFields("B")
field.Orientation = xlRowField
field.Position = 1

Set field = wsTarget.PivotTables("PivotTable1").PivotFields("TOTAL")
Set field = wsTarget.PivotTables("PivotTable1").AddDataField(field, "Sum of TOTAL", xlSum)
field.NumberFormat = "_ $ * #,##0.00_ "

Application.ScreenUpdating = True

the code keeps crashing at the "Set pc" line, and i can't figure out why... tried googling, and all of the results were identical to my code... Important detail - I'm working with excel 2013, maybe it's because of that... Will appreciate any help I can get - Thank you!


Viewing all articles
Browse latest Browse all 88066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>