How to Run a Sort When a Calculation Occurs (152383)



The information in this article applies to:

  • Microsoft Excel for Windows 95 7.0a
  • Microsoft Excel for Windows 5.0c
  • Microsoft Excel for the Macintosh 5.0a

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

  1. 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:
    						
  2. 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

  3. 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
    						
  4. 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.
    • Quit Microsoft Excel by clicking on Exit on the File menu.
Additional query: 5.0 5.0a 5.0c 7.0 7.0a vba

Modification Type:MinorLast Reviewed:10/10/2006
Keywords:kbcode kbhowto kbProgramming KB152383