How to Subtotal a Variable Number of Columns Using VBA in Excel
Question details
The user needs a VBA macro capable of dynamically detecting the last used column to apply subtotals across a changing data range, rather than relying on a hardcoded, fixed column array.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating subtotals on changing data sets where the starting column remains consistent, but the end column varies from report to report.
- Observed behavior
- The goal state is to programmatically identify the final populated column, dynamically build an array of column indices, and successfully pass this array into the Subtotal method.
Ensure you have the Developer tab enabled in your spreadsheet application to access the VBA editor. It is highly recommended to back up your workbook before running new macros to prevent accidental data overwrites.
Dynamically Detect Last Column and Apply Subtotal
Use the Cells.Find method to locate the last used column and ReDim to dynamically build the subtotal array for the active range.
Instead of hardcoding the columns you want to subtotal, you can use VBA to search the worksheet for the last cell containing data. By combining this with dynamic array resizing, your macro will automatically adapt regardless of how many columns of data you add or remove.
Press ALT + F11 to launch the Visual Basic for Applications editor. Go to Insert > Module to create a new script area.
Declare a variable for the last column and assign it using the Find method: LastCol = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column.
Declare an integer array (e.g., Dim SubArray() As Integer). Use ReDim SubArray(1 To (LastCol - StartCol + 1)) to size the array based on the number of numeric columns you need to calculate.
Write a For loop that iterates from 1 to the size of your array, assigning the correct column index values to each array element.
Apply the subtotal to your target range by passing the populated array into the TotalList parameter, for example: Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=SubArray, Replace:=True.

Execute VBA Macros and Subtotals Natively in WPS Spreadsheet
WPS Spreadsheet offers robust support for VBA macros, allowing you to seamlessly run dynamic subtotal scripts and automate repetitive data processing tasks with the same familiar syntax.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled workbook (.xlsm).
- 2. Access Developer Tools: Navigate to the 'Developer' tab on the main ribbon to manage and edit your macros.
- 3. Run the Subtotal Macro: Click 'Macros', select your dynamic subtotal script from the list, and click 'Run' to apply the subtotals across your variable dataset.

Frequently Asked Questions
Why is the VBA Find method returning the wrong last column?
This usually happens if there are hidden columns or differently formatted empty cells. To fix this, ensure your parameters are set to SearchOrder:=xlByColumns and SearchDirection:=xlPrevious, and consider adding LookIn:=xlFormulas to ensure hidden formula results are not skipped.
Can I use this dynamic array method for nested subtotals?
Yes. You can apply multiple layers of subtotals by repeatedly passing your dynamic array into the Subtotal method while setting the 'Replace' argument to False.
How do I prevent my macro from crashing if the worksheet is empty?
Before assigning the LastCol variable, set a Range object to the result of your Cells.Find method. Check if the object is 'Nothing'. If it is not 'Nothing', proceed to extract the Column property; otherwise, exit the sub.
Does WPS Office support VBA and the Subtotal method?
Yes, WPS Spreadsheet fully supports VBA environments (in Professional and Macro-enabled versions) and recognizes standard object methods like Subtotal, ReDim, and Cells.Find for dynamic array sizing.




