Yes, there is a way to get a subtotal in the group in a pivot table. Here are the steps:
- Select the data range that you want to create a pivot table from.
- Go to the Insert tab and click on PivotTable.
- In the Create PivotTable dialog box, select the location where you want to place the pivot table and click OK.
- In the PivotTable Fields pane, drag the Accounting Fund Code field to the Rows area and the Amount field to the Values area.
- Click on the drop-down arrow next to the Accounting Fund Code field in the Rows area and select Group.
- In the Grouping dialog box, specify the starting and ending values for the group and the interval, if necessary. Click OK.
- The pivot table will now display the subtotals for each group.
To autogenerate pivot tables in a new workbook, you can use a macro. Here's an example macro that creates a pivot table from a named range and places it in a new worksheet:
- Press Alt + F11 to open the Visual Basic Editor.
- In the Project window, right-click on the workbook where you want to create the macro and select Insert Module.
- Paste the following code into the module:
Sub CreatePivotTable()
Dim rng As Range
Dim pt As PivotTable
Dim ws As Worksheet
'Change "DataRange" to the name of your data range
Set rng = ThisWorkbook.Names("DataRange").RefersToRange
'Change "PivotTableLocation" to the location where you want to place the pivot table
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets (ThisWorkbook.Sheets.Count))
ws.Name = "PivotTable"
Set pt = ws.PivotTableWizard(TableDestination:=ws.Range("A3 "), TableName:="PivotTable")
'Add fields to the pivot table
With pt
.PivotFields("Accounting Fund Code").Orientation = xlRowField
.PivotFields("Amount").Orientation = xlDataField
End With
'Group the rows in the pivot table
With pt.PivotFields("Accounting Fund Code")
.Orientation = xlRowField
.Position = 1
.Group Start:=1, End:=4, By:=2
End With
End Sub
- Save the macro and close the Visual Basic Editor.
- To run the macro, go to the Developer tab (if it's not visible, go to File Options Customize Ribbon and check Developer in the right-hand pane) and click on Macros. Select the macro and click Run.
This macro creates a new worksheet called "PivotTable" and places the pivot table in cell A3. You can modify the code to suit your needs, such as changing the data range and pivot table location.