VBA & Macro Problems

How to Insert Blank Rows and Calculate Subtotals using Excel VBA

Olivia MillerOlivia Miller Nov 3, 2026 870 views

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.

How to Insert Blank Rows and Calculate Subtotals Using Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor, then insert a new Module from the Insert menu.

2
Write the Subtotal Code

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.

3
Run the Macro

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 the Built-in Subtotal Method in VBA
Dynamic Range Suggestion: Instead of hardcoding a range like Range("A3:G16"), use a dynamic range variable (e.g., Range("A3:G" & Cells(Rows.Count, "A").End(xlUp).Row)) so the macro works regardless of data size.

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. 1. Open Your Data File in WPS: Launch WPS Spreadsheet and open the workbook where you need to insert subtotals.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon. If it is hidden, enable it in the WPS options.
  3. 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. 4. Execute the Code: Click the Run button or press F5 to execute the script and watch your data group and calculate automatically.
Seamless execution of standard Excel VBA macro codes.Fully compatible with Microsoft Excel .xlsm and .xlsb formats.Intuitive Developer tab for easy macro management and debugging.Lightweight and free to use for efficient daily office tasks.
microsoft office alternative - wps office

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.