VBA & Macro Problems

How to Subtotal a Variable Number of Columns Using VBA in Excel

Rana GarciaRana Garcia Nov 4, 2026 869 views

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.

VBA Macro to Subtotal a Variable Number of Excel Columns
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 to launch the Visual Basic for Applications editor. Go to Insert > Module to create a new script area.

2
Identify the Last Column

Declare a variable for the last column and assign it using the Find method: LastCol = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column.

3
Define the Dynamic Array

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.

4
Populate the Array

Write a For loop that iterates from 1 to the size of your array, assigning the correct column index values to each array element.

5
Execute the Subtotal Method

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.

Dynamically Detect Last Column and Apply Subtotal
Adaptive Macro Execution: By utilizing dynamic arrays, your macro operates safely and prevents 'Subscript out of range' errors when dealing with unpredictably wide datasets.
Advanced Data Processing in WPS Office

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. 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled workbook (.xlsm).
  2. 2. Access Developer Tools: Navigate to the 'Developer' tab on the main ribbon to manage and edit your macros.
  3. 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.
Seamless execution of standard VBA macros and Subtotal methodsHigh compatibility with Microsoft Excel .xlsm and .xlsb formatsLightweight architecture handles large arrays and dynamic data smoothlyFamiliar Developer UI requires zero learning curve
microsoft office alternative - wps office

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.