Removing Subtotals from a pivot tables using vba

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>
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,216,073
Messages
6,128,644
Members
449,461
Latest member
kokoanutt

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