3.4 Executing Macros


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 a macro is available only in the development environment. In general, one uses the command TOOLSMACRO, which is available both in Excel and in the development environment. With it you can run parameterless macros defined in any currently open file.

There are also a number of elegant methods for running macros:

  • You can associate a keyboard shortcut of the form Ctrl+first letter of the macro to a macro by clicking on the Options button Ctrl+first letter in the form ToolsMacroMacros. For some strange reason this Options button is available only in Excel, not in the development environment. Go figure.

  • 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 ViewToolbarsCustomizeCommands (still in Excel) you can insert the menu entry "Custom Menu Item" or "Custom Button" (both in the group "macros") to a menu or toolbar. Then you can alter the menu text or tool with the associated pop-up menu. By clicking on the entry or tool for the first time you can select the macro to which the entry will be associated in the future. More information on individual menus and toolbars is to be found in Chapter 8.

These methods hold for procedural macros, those introduced with the 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 .

Interrupting Macros

All macros can be stopped at any time with Ctrl+Break . If you then select the button Debug in 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

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 ViewImmediate Window or with Ctrl+G .

The immediate window contains the last two 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 Print method. Floating point numbers are generally displayed in the immediate window to at most eight places beyond the decimal point, even when sixteen places exist.

In the immediate window it is also possible to assign values to variables or properties or to start macros by giving their name. In the immediate window you can introduce new variables without a previous declaration (even if Option Explicit is in force in the program code).

Tip  

The watch window makes possible the continuous display of the contents of various properties or variables. The use of this window will be described extensively in Chapter 6, where debugging is discussed. There we will also describe the window for the display of all active procedures.




Definitive Guide to Excel VBA
Linux Application Development For The Enterprise (Charles River Media Programming)
ISBN: 1584502533
EAN: 2147483647
Year: 2002
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net