VBA & Macro Problems

Sort Totals on a Separate Worksheet with Excel VBA

Anum MushtaqAnum Mushtaq Nov 25, 2026 869 views

Question details

Automatically sort calculated totals on a separate worksheet when source data changes.

Sort Totals on a Separate Worksheet with Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A Totals worksheet calculates values from three other worksheets, some of which have merged headers.
Observed behavior
A Worksheet_Change macro may not run when formulas on the Totals sheet recalculate, and the original worksheet or sort range references may not match the workbook layout.
Before you start

Identify the full range of records and the column containing the totals on the Totals sheet. Make sure the rows to be sorted contain no merged cells, and check that moving the records will not invalidate their formulas or references.

Solution 1Recommended

Sort the Totals sheet when its formulas recalculate

Use a Worksheet_Calculate event when the totals are formula results rather than values entered directly on the Totals sheet.

The example sorts A4:Z100 by the values in column D, highest first. These are placeholders: replace them with the actual record range and totals column on the Totals sheet. The range should include every column that must move with each record, but exclude title and header rows.

Worksheet_Calculate runs after the sheet recalculates, including recalculations that do not change the totals. A guard and disabled events prevent the sort from recursively triggering the event.

1
Open the Totals sheet module

In Excel, press Alt+F11 to open the VBA editor. In Project Explorer, find your workbook and double-click the Totals worksheet under Microsoft Excel Objects.

2
Add the calculation event

Paste this code into the Totals worksheet module: Private Sub Worksheet_Calculate() Static busy As Boolean Dim oldEvents As Boolean If busy Then Exit Sub busy = True oldEvents = Application.EnableEvents On Error GoTo CleanUp Application.EnableEvents = False Me.Range("A4:Z100").Sort Key1:=Me.Range("D4"), _ Order1:=xlDescending, Header:=xlNo, _ Orientation:=xlTopToBottom CleanUp: Application.EnableEvents = oldEvents busy = False End Sub

3
Adjust the sort range

In the pasted code, replace A4:Z100 with the full data range on Totals and D4 with the first cell in the totals column. Keep merged titles and headers outside the range; unmerge any cells within the records before sorting.

4
Save and test

Use File > Save As to save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Enable macros if prompted, change a source value, and confirm that the Totals rows sort correctly after recalculation.

Sort the Totals sheet when its formulas recalculate
Check calculation settings: If Excel uses manual calculation, press F9 to recalculate and trigger the event. The event may run more often than expected, so limit the sort range to the records that need sorting.
Free Microsoft Office alternative

Sort spreadsheet totals with WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files and use the sort controls to organize totals; check macro support separately before migrating an event-driven VBA workbook.

Sort totals manually without creating an Excel VBA eventOpen and edit Microsoft Excel formats such as XLSX and XLSUse a familiar spreadsheet interface for an easier transitionWork with a free, lightweight Office alternative
QA img-9

Frequently Asked Questions

Why doesn't Worksheet_Change run when a formula result changes?

Worksheet_Change responds to changes to cells, not to formula results changing during recalculation. Put a Worksheet_Calculate event in the Totals sheet module to respond when that sheet recalculates.

Can I sort a range that contains merged cells?

Merged cells can prevent Excel from sorting reliably. Exclude merged title and header rows from the sort range, and unmerge cells within the records before sorting.

What if my totals are in a different column?

In the code, replace Me.Range("D4") with the first cell of your totals column. Also update Me.Range("A4:Z100") to include the full rows of data you want to move together.

Why isn't the sort running after I change a source sheet?

Confirm that macros are enabled and that the code is in the Totals worksheet module. If calculation is set to Manual, press F9 or change the setting under Formulas > Calculation Options.