VBA add row field not working

tazeo

Board Regular
Joined
Feb 15, 2007
Messages
133
Office Version
  1. 365
Platform
  1. 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.


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
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Below should work
Dim newSheet As Worksheet
Set newSheet = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
Dim pivotCache As pivotCache
Set pivotCache = ThisWorkbook.PivotCaches.Create(xlDatabase, ThisWorkbook.Worksheets(1).ListObjects("Table1"))
Dim pivotTable As pivotTable
Set pivotTable = pivotCache.CreatePivotTable(TableDestination:=newSheet.Range("A3"), TableName:="PivotTable1")
pivotTable.PivotFields("Functional Location").Orientation = xlRowField
pivotTable.PivotFields("Incident Classification").Orientation = xlColumnField
pivotTable.AddDataField pivotTable.PivotFields("Incident ID"), "Count of Incident ID", xlCount
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top