How to Insert Blank Rows and Calculate Subtotals using Excel VBA
Question details
The user needs to use Excel VBA to group rows based on unique values in a specific column, insert totals or blank rows beneath them, and automatically calculate sums for specific numeric columns.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the grouping and summing of data ranges based on unique values, eliminating the need to manually insert rows and calculate totals.
- Observed behavior
- Requires an efficient VBA macro to either use native subtotal features or loop backwards to insert formatted blank rows and customized sum formulas below grouped data.
Ensure that your data is correctly sorted by the column you wish to group by (e.g., Column B) before running any subtotal macros. You will also need to enable the Developer tab to access the Visual Basic editor.
Use the Built-in Subtotal Method in VBA
The most efficient way to group data and add sums is by leveraging Excel's native Subtotal function within a macro, which calculates dynamically based on the last-used row.
This method uses the standard Selection.Subtotal command, allowing you to group by a specified column and apply the xlSum function to multiple target columns.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor, then insert a new Module from the Insert menu.
Create a new sub-procedure. Select your data range dynamically, then use the command: Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(6, 7), Replace:=True, SummaryBelowData:=True. Adjust the GroupBy index and TotalList array according to your column layout.
Press F5 or run the macro from the Developer tab. Excel will automatically group rows by Column B and insert subtotal rows for columns 6 and 7.

Use a Backward Loop for Custom Blank Rows and Sums
If you need exactly two blank rows and a custom 'Total' label without triggering Excel's native grouping feature, you can use a custom VBA loop.
Create a Macro to Remove Subtotals
It is best practice to have a secondary macro designed to quickly clear subtotals and revert your dataset to its raw form.
Automate Data Subtotals with Macros in WPS Spreadsheet
WPS Spreadsheet provides powerful support for VBA and Excel macros. You can easily write, edit, and execute your VBA scripts to automatically insert blank rows and calculate subtotals, boosting your productivity.
- 1. Open Your Data File in WPS: Launch WPS Spreadsheet and open the workbook where you need to insert subtotals.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon. If it is hidden, enable it in the WPS options.
- 3. Open the Visual Basic Editor: Click the 'Visual Basic' icon to launch the editor. Insert a new Module and paste your Subtotal macro code.
- 4. Execute the Code: Click the Run button or press F5 to execute the script and watch your data group and calculate automatically.

Frequently Asked Questions
Do I need to sort my data before running a subtotal macro?
Yes. The Subtotal method relies on identical values being grouped together consecutively. You must sort your data by the target grouping column before executing the macro, otherwise it will create separate subtotals for every value change.
Why does my macro throw a runtime error on the Subtotal method?
This usually occurs if the selected range is incorrect, lacks headers, or if the worksheet is protected. Ensure your macro accurately selects the data range, including headers, and that sheet protection is disabled before running.
Can I sum multiple columns at once using VBA?
Yes, you can pass an array of column indices to the TotalList parameter. For example, using TotalList:=Array(6, 7) will simultaneously calculate sums for the 6th and 7th columns of your selection.
How do I dynamically find the last row in my macro?
You can dynamically determine the last row of a dataset by using the code snippet: LastRow = Cells(Rows.Count, 1).End(xlUp).Row. Replace '1' with the appropriate column index if your key data is in another column.




