tazeo
Board Regular
- Joined
- Feb 15, 2007
- Messages
- 133
- Office Version
- 365
- Platform
- Windows
I've got some VBA code in a macro to auto-create a workbook and create a Pivot table (below).
The whole thing works like a dream, and throws no errors, creates everything but I can't get it to add the row field to the pivot table.
I found the script a while ago and saved it for later use, but I can't find the original source to try to backtrack why it isn't working.
The whole thing works like a dream, and throws no errors, creates everything but I can't get it to add the row field to the pivot table.
I found the script a while ago and saved it for later use, but I can't find the original source to try to backtrack why it isn't working.
VBA Code:
' Create a new sheet in the new workbook and add a pivot table
Dim newSheet As Worksheet
Set newSheet = newWorkbook.Worksheets.Add(After:=newWorkbook.Worksheets(newWorkbook.Worksheets.Count))
Dim pivotCache As pivotCache
Set pivotCache = newWorkbook.PivotCaches.Create(xlDatabase, newWorkbook.Worksheets(1).ListObjects("Table1"))
Dim pivotTable As pivotTable
Set pivotTable = pivotCache.CreatePivotTable(TableDestination:=newSheet.Range("A3"), TableName:="PivotTable1")
pivotTable.AddFields RowFields:="Functional Location"
pivotTable.PivotFields("Functional Location").Orientation = xlRowField
pivotTable.AddFields ColumnFields:="Incident Classification"
pivotTable.AddDataField pivotTable.PivotFields("Incident ID"), "Count of Incident ID", xlCount