Читать книгу Excel VBA Programming For Dummies - Dick Kusleika - Страница 59

COMPILE ERROR?

Оглавление

There’s a chance that the GuessName macro won’t work. When you try to run it, Excel may complain and pop up an error message: Compile Error: Variable Not Defined. Don’t worry; there’s an easy fix for that.

If you receive that error, look at the top of your module, and you’ll see this text: Option Explicit. Just delete that line, and the macro should work. That line, when present at the top of a module, means that you must “declare” all your variables. (See Chapter 7 for more about variables.) If that line was added, it means that your VBE is set up to add the line automatically. For now, don’t worry about it. Delete the line, and forget about the rude interruption.

If you followed the previous steps, you just wrote a VBA Sub procedure, also known as a macro. When you press F5, Excel executes the code and follows the instructions. In other words, Excel evaluates each statement and does what you told it to do. (Don’t let this newfound power go to your head.) You can execute this macro any number of times — although it tends to lose its appeal after a few dozen times.

For the record, the following concepts were used to create this simple macro, all of which are covered later in this book:

 Defining a Sub procedure (the first line)

 Assigning values to variables (Msg and Ans)

 Concatenating (joining) a string (using the & operator)

 Using a built-in VBA function (MsgBox)

 Using built-in VBA constants (vbYesNo, vbNo, and vbYes)

 Using an If-Then construct (twice)

 Ending a Sub procedure (the last line)

Not bad for a beginner, eh?

Excel VBA Programming For Dummies

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