Microsoft KB Archive/152383

= How to Run a Sort When a Calculation Occurs =

Article ID: 152383

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 95a
 * Microsoft Excel 5.0c
 * Microsoft Excel 5.0a for Macintosh

-



This article was previously published under Q152383



SUMMARY
In Microsoft Excel, you can sort data when a calculation occurs on a worksheet by using the OnCalculate property.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Sample Visual Basic Procedure
  Enter the following in a worksheet:   A1: Stock Name  B1: Yesterday's Quote  C1: Today's Quote     D1: Change A2: ABC        B2: 2.00               C2: 3.00              D2: A3: LMO        B3: 1.00               C3: 0.50              D3: A4: UVW        B4: 5.00               C4: 9.00              D4: A5: XYZ        B5: 12.00              C5: 11.50             D5:  Type the following formula in cell D2:

D2: =C2-B2

With cell D2 selected, grab the fill handle and fill down the formula through cell D5. The resulting cells will look as follows:

D1: Change

D2: 1.00

D3: -0.50

D4: 4.00

D5: -0.50

  Enter the following macro code in a module sheet: Sub ActivateSortRoutine Workbooks("book1").Sheets("sheet1").OnCalculate = "SortRoutine" ' This sets the OnCalculate property. Replace book1 with your ' workbook including the file extension, if any, and sheet1 with ' the name of your sheet. End Sub

Sub SortRoutine Range(ActiveCell.Address).CurrentRegion.Select ' Selects list. Selection.Sort Key1:=Range("d2"), Order1:=xlDescending, Header:= _ xlYes, OrderCustom:=1, MatchCase:=False, Orientation:= _ xlTopToBottom ' This will do a descending sort, based on data in column D,        ' ignoring the header row. Range("d1").Select ' Clears highlight of list and selects the header of data in        ' column D.      End Sub

Sub StopActivateSortRoutine ActiveWorkbook.ActiveSheet.OnCalculate = "" ' Stop running the CallSortRoutine macro. End Sub  To run the ActivateSortRoutine macro, on the Tools menu click Macro, select ActivateSortRoutine, and then click Run.

After you run the ActivateSortRoutine, the SortRoutine macro will run when a calculation occurs on the worksheet. To stop the sorting, choose one of the three following methods:

 Run the StopActivateSortRoutine macro. To do so, click Macro on the Tools menu, select "StopActivateSortRoutine", and click Run. Change the calculation mode to manual. To do so, click Options on the Tools menu, click the Calculation Tab, select Manual, and then click OK.</li> Quit Microsoft Excel by clicking on Exit on the File menu.</li></ul> </li></ol>

Additional query: 5.0 5.0a 5.0c 7.0 7.0a vba

Additional query words: XL

Keywords: kbcode kbhowto kbprogramming KB152383

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.