Читать книгу Excel 2019 Power Programming with VBA - Michael Alexander, Dick Kusleika - Страница 121

About the code examples

Оглавление

Throughout this book, we present many small snippets of VBA code to make a point or to provide an example. In some cases, this code consists of a single statement, or only an expression, which isn't a valid instruction by itself.

For example, the following is an expression:

Range("A1").Value

To test an expression, you must evaluate it. The MsgBox function is a handy tool for this:

MsgBox Range("A1").Value

To try these examples, put the statement in a procedure in a VBA module, like this:

Sub Test() ' statement goes here End Sub

Then put the cursor anywhere in the procedure and press F5 to execute it. Also, make sure that the code is being executed in the proper context. For example, if a statement refers to Sheet1, make sure that the active workbook has a sheet named Sheet1.

If the code is just a single statement, you can use the VBE Immediate window. The Immediate window is useful for executing a statement immediately, without having to create a procedure. If the Immediate window isn't displayed, press Ctrl+G in the VBE.

Just type the VBA statement in the Immediate window and press Enter. To evaluate an expression in the Immediate window, precede the expression with a question mark (?), which is a shortcut for Print. For example, you can type the following in the Immediate window:

? Range("A1").Value

The result of this expression is displayed in the next line of the Immediate window.

Excel 2019 Power Programming with VBA

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