Not showing sub-totals in Pivot table using VBA

g4cox

New Member
Joined
Sep 9, 2009
Messages
41
Hi,

For some reason I am getting ‘run-time error 1004 unable to set the subtotals properly of the pivotfield class’ when I run the below code.

It appears to run fine upto 12 but on the 13th it comes up with this error can anyone help?

Code:
Dim ptf As PivotField

With ActiveSheet.PivotTables("OOH") ' Switching off subtotals
        For Each ptf In .PivotFields
        ptf.Subtotals(1) = True
        ptf.Subtotals(1) = False
        Next ptf
    End With

This is the full code for pivot table creation.

Code:
    'Inserting Pivot table
    Dim ws, pivws As Worksheet
    Dim ptc As PivotCache
    Dim pt As PivotTable
    Dim ptf As PivotField
    Dim ptr As Range
    Dim lr As Long
    Set ws = Worksheets("Summarised Data")
    Set pivws = Worksheets("PivotData")
    ws.Select
    lr = ws.Cells(Rows.Count, 2).End(xlUp).Row
    Set ptr = Range("B1:M" & lr & "")
    pivws.Select


    Set ptc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=ptr, Version:=xlPivotTableVersion14)


    Set pt = ptc.CreatePivotTable(TableDestination:=pivws.Cells(2, 2), _
    TableName:="OOH")
    
    With ActiveSheet.PivotTables("OOH").PivotFields("Month")
        .Orientation = xlRowField
        .Position = 1
    End With


    With ActiveSheet.PivotTables("OOH").PivotFields("Day")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("OOH").PivotFields("Location")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("OOH").PivotFields("Shift")
        .Orientation = xlRowField
        .Position = 3
    End With


    ActiveSheet.PivotTables("OOH").AddDataField ActiveSheet.PivotTables( _
        "OOH").PivotFields("No. of Base visits"), "Average number of base visits", xlSum


    ActiveSheet.PivotTables("OOH").AddDataField ActiveSheet.PivotTables( _
        "OOH").PivotFields("Time BV"), "Number of hours to see base visits", xlSum


    ActiveSheet.PivotTables("OOH").AddDataField ActiveSheet.PivotTables( _
        "OOH").PivotFields("Time HV"), "Number of hours to do home visits", xlSum


    With ActiveSheet.PivotTables("OOH").PivotFields("Average number of base visits")
        .NumberFormat = "#,##0.00"
    End With


    With ActiveSheet.PivotTables("OOH").PivotFields("Number of hours to see base visits")
        .NumberFormat = "#,##0.00"
    End With


    With ActiveSheet.PivotTables("OOH").PivotFields("Number of hours to do home visits")
        .NumberFormat = "#,##0.00"
    End With


        ActiveSheet.PivotTables("OOH").NullString = "0"


    With ActiveSheet.PivotTables("OOH").PivotFields("Shift")
        .PivotItems("Night Shift").Visible = False
'        .PivotItems("Not OOH").Visible = False
    End With
    ActiveSheet.PivotTables("OOH").PivotFields("Shift").PivotItems("Morning Shift") _
        .Position = 1
        
    With ActiveSheet.PivotTables("OOH") ' Switching off subtotals
        For Each ptf In .PivotFields
        ptf.Subtotals(1) = True
        ptf.Subtotals(1) = False
        Next ptf
    End With

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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