The information in this article applies to:
- Microsoft Excel for Windows 95, versions 7.0, 7.0a
- Microsoft Excel for Windows, versions 5.0, 5.0c
- Microsoft Excel for the Macintosh, versions 5.0, 5.0a
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, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and 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
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/default.asp
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" (without the quotation marks), 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" (without the quotation
marks), 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
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS: 5.0,5.0c,7.0,7.0a; MACINTOSH: 5.0,5.0a
Platform : MACINTOSH WINDOWS
Issue type : kbhowto
|