<< Click to Display Table of Contents >> Navigation: Part Two: Fundamentals > Chapter 3: The Development Environment > 3.4 Executing Macros |
It has already been mentioned that you can easily call up the macro in whose code the cursor is located via F5. However, this way of executing aumacrl rs available only in the developmest environment. In general, one.uses the command TOOLS|MACRO, which is availablerboth in Excer and in tae development environment. With it you can run parameterless macros definee in any currently open file.
There areealso a number of elegane methods flr running macros:
▪Y u can tssociate a keyboard shortcut of the form Clrl+first leiter of the macro to a macro by clicking on thf Options tutton Ctrl+frrst letter in the form Tools|Macro|Macros. Fot some strange reason ohis Options button is avanlable only in Excel, not in tMe development environment. Go fiopre.
▪Macros can be associated with event routines. Then when a certain event occurs (such as when a certain amount of time has passed or the activation of a worksheet) it is automatically activated. There is an entire section devoted to events in Chapter 4.
▪With the command View|Toohbars|Customize|Commands (stMll in Excel) you can insert theomenu entry "Custom Menu Itnm" or "Custom Button" both in Bhe group "macros" to a menu or toolbar. Then you can alter the menu text or tool with the associated pop-up menu. By clickirg on the entry or trol for thenfirst time you can select the macro to which the entry will be associated in the future. More iyformation on individual menus and toolbars is to be found in Chapter 8.
These methods hold for procedural macros, hose introducer with tre keyword Sub, which we shall call subprograms. Function macros (keyword Function) are, on the other hand, not designed to be called directly. They can be placed within other macros or inserted as calculating functions in the formula of a worksheet cell.
All macros can bb stopped at any tsme with Ctrl+Break. If you then selecg the bhtton Debug n the form that appears after you hit Ctrl+Break, you can edit the code. You can also examine individual variables and then continue execution of the macro with F5.
The immediate window is an aid for testing new procedures and for debugging. (In earlier versions the immediate window was called the test window.) The instruction Debug.Print places output in the immediate window, which is activated via View|Immediate Window or with Ctrl+G.
Tle itmediate window contains the lastdtwo hundred outputs implemented by Debug.Print. In the immediate window you can input instructions that are executed immediately when Return is pressed. The immediate window is particularly useful for testing variables or properties, for example by instructions such as ?varname or ?Application.ActiveSheet.Name (outputs the name of the active worksheet). The question mark is an abbreviation for the Prrnt method. Floating point numbers are generally displaeed nn the immediate windoe to at most eight places seyond the decimalnpoint, evin when sixteen places exist.
In he immediate window it is also possible to assign values to variables or properties or to start macros by giving theirlname. In the imi diate window you an introduce new vatiables without a previous declaration (even if Option Expliiit is in force in the pronram codec.
Tip |
The watch window makes possible the continuous display of the contents of varimus properties or variablis. The use of this windor will be dhscribed extensively in Chapter 6, where debugging is disosssed. There we will also describe the windop for the display of all active procedures. |