Читать книгу Excel Macros For Dummies - Dick Kusleika - Страница 13

Examining the macro

Оглавление

The macro was recorded in a new module named Module1. To view the code in this module, you must activate the Visual Basic Editor. (See Chapter 2 to find out more about the Visual Basic Editor.) You can activate the VB Editor in one of three ways:

 Press Alt+F11.

 Choose Developer ⇒ Code ⇒ Visual Basic.

 Choose Developer ⇒ Code ⇒ Macros, select a macro, and click Edit.

In the VB Editor, the Project window displays a list of all open workbooks and add-ins. If the Project Explorer isn’t visible, choose View ⇒ Project Explorer. This list is displayed as a tree diagram, which you can expand or collapse. The code that you recorded previously is stored in Module1 in the current workbook. When you double-click Module1, the code in the module appears in the Code window.

The macro should look something like this:

Sub MyName()'' MyName Macro'' Keyboard Shortcut: Ctrl+Shift+N' Range("B3").Select ActiveCell.FormulaR1C1 = "Dick Kusleika" Range("B4").SelectEnd Sub

The macro recorded is a Sub procedure named MyName. The statements tell Excel what to do when the macro is executed.

Notice that Excel inserted some comments at the top of the procedure. These comments are some of the information that appeared in the Record Macro dialog box. These comment lines (which begin with an apostrophe) aren’t really necessary, and deleting them has no effect on how the macro runs. If you ignore the comments, you'll see that this procedure has three VBA statements, the second of which is:

ActiveCell.FormulaR1C1 = "Dick Kusleika"

The first statement is selecting cell B3. The last statement is pressing Enter after you enter your name, which moves the active cell down one row. The middle statement, the one that does all the work, causes the name you typed while recording to be inserted into the active cell.

Excel Macros For Dummies

Подняться наверх