yuvarajp90
New Member
- Joined
- Jul 29, 2016
- Messages
- 1
I have been using a vba code to create pivot tables and remove grand & sub-totals for a couple of weeks now. The subtotal part of the code which was previously working well is not throwing the following error
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Run-Time Error'1004': Unable to set the Subtotals property of the PivotField Class</code>and i an not able to figure whats causing it.
The code for the subtotal is going into a loop and then throwing the above error.
Below is the code, Appreciate any help.
Regards
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Sub Pivottable_5()
Dim objTable As PivotTable
Dim objField As PivotField
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("All Proj").Select
Range("A1").Select
' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = ActiveSheet.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("Products")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Product")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Value Category")
objField.Orientation = xlRowField
objField.PivotItems("(blank)").Visible =False
Set objField = objTable.PivotFields("Project Status")
objField.Orientation = xlPageField
objField.PivotItems("Cancelled").Visible =False
objField.PivotItems("Suspended").Visible =False
objField.PivotItems("Scoped not active").Visible =False
objField.PivotItems("(blank)").Visible =False
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("2014 Actual Total")
objField.Orientation = xlDataField
objField.Function= xlSum
objField.NumberFormat ="$ #,##0"
Set objField = objTable.PivotFields("2015 Actual Total")
objField.Orientation = xlDataField
objField.Function= xlSum
objField.NumberFormat ="$ #,##0"
Set objField = objTable.PivotFields("2016 Actual Total")
objField.Orientation = xlDataField
objField.Function= xlSum
objField.NumberFormat ="$ #,##0"
ActiveSheet.name ="iNexus Pivot"
' Rename the pivot table
With Sheets("iNexus Pivot")
.PivotTables(1).name ="PivotTable2"
EndWith
With Sheets("iNexus Pivot").PivotTables("PivotTable2").DataPivotField
.Orientation = xlColumnField
EndWith
ActiveSheet.PivotTables("PivotTable2").ColumnGrand =False
ActiveSheet.PivotTables("PivotTable2").RowGrand =False
Dim i AsInteger
Dim iFieldMax AsInteger
'Find the number of PivotFields
iFieldMax = ActiveSheet.PivotTables("PivotTable2").PivotFields.Count
'Loop through the fields in the Pivot
For i =1To iFieldMax
With ActiveSheet.PivotTables("PivotTable2").PivotFields(i)
'Set subtotal calculation to nothing
.Subtotals = Array(False,False,False,False,False,False,False,False,False,False,False,False)
EndWith
Next i
ActiveSheet.PivotTables("PivotTable2").RowAxisLayout xlTabularRow
EndSub</code>
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Run-Time Error'1004': Unable to set the Subtotals property of the PivotField Class</code>and i an not able to figure whats causing it.
The code for the subtotal is going into a loop and then throwing the above error.
Below is the code, Appreciate any help.
Regards
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Sub Pivottable_5()
Dim objTable As PivotTable
Dim objField As PivotField
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("All Proj").Select
Range("A1").Select
' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = ActiveSheet.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("Products")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Product")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Value Category")
objField.Orientation = xlRowField
objField.PivotItems("(blank)").Visible =False
Set objField = objTable.PivotFields("Project Status")
objField.Orientation = xlPageField
objField.PivotItems("Cancelled").Visible =False
objField.PivotItems("Suspended").Visible =False
objField.PivotItems("Scoped not active").Visible =False
objField.PivotItems("(blank)").Visible =False
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("2014 Actual Total")
objField.Orientation = xlDataField
objField.Function= xlSum
objField.NumberFormat ="$ #,##0"
Set objField = objTable.PivotFields("2015 Actual Total")
objField.Orientation = xlDataField
objField.Function= xlSum
objField.NumberFormat ="$ #,##0"
Set objField = objTable.PivotFields("2016 Actual Total")
objField.Orientation = xlDataField
objField.Function= xlSum
objField.NumberFormat ="$ #,##0"
ActiveSheet.name ="iNexus Pivot"
' Rename the pivot table
With Sheets("iNexus Pivot")
.PivotTables(1).name ="PivotTable2"
EndWith
With Sheets("iNexus Pivot").PivotTables("PivotTable2").DataPivotField
.Orientation = xlColumnField
EndWith
ActiveSheet.PivotTables("PivotTable2").ColumnGrand =False
ActiveSheet.PivotTables("PivotTable2").RowGrand =False
Dim i AsInteger
Dim iFieldMax AsInteger
'Find the number of PivotFields
iFieldMax = ActiveSheet.PivotTables("PivotTable2").PivotFields.Count
'Loop through the fields in the Pivot
For i =1To iFieldMax
With ActiveSheet.PivotTables("PivotTable2").PivotFields(i)
'Set subtotal calculation to nothing
.Subtotals = Array(False,False,False,False,False,False,False,False,False,False,False,False)
EndWith
Next i
ActiveSheet.PivotTables("PivotTable2").RowAxisLayout xlTabularRow
EndSub</code>