11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Storing VBA code<br />

In general, a module can hold three types of code:<br />

Chapter 22: Introducing VBA 609<br />

Sub procedures: A procedure is a set of instructions that performs some action. For<br />

example, you may have a Sub procedure that combines various parts of a workbook into<br />

a concise report.<br />

Function procedures: A function is a set of instructions that returns a single value or an<br />

array. You can use Function procedures in worksheet formulas.<br />

Declarations: A declaration is information about a variable that you provide to VBA. For<br />

example, you can declare the data type for variables that you plan to use. Declarations<br />

go at the top of the module.<br />

A single VBA module can store any number of procedures and declarations.<br />

This book focuses exclusively on Function procedures, which are the only type of<br />

procedure that you can use in worksheet formulas.<br />

Entering VBA code<br />

This section describes the various ways of entering VBA code in a code window. For Function<br />

procedures, the code window is always a VBA module. You can add code to a VBA module in<br />

three ways:<br />

Use your keyboard to type it.<br />

Use the Excel macro-recorder feature to record your actions and convert them into<br />

VBA code.<br />

Copy the code from another module and paste it into the module that you are working on.<br />

Entering code manually<br />

Sometimes, the most direct route is the best one. Type the code by using your keyboard.<br />

Entering and editing text in a VBA module works just as you expect. You can select text and<br />

copy it, or cut and paste it to another location.<br />

Use the Tab key to indent the lines that logically belong together — for example, the conditional<br />

statements between an If and an End If statement. Indentation is not necessary, but it makes<br />

the code easier to read.<br />

A single instruction in VBA can be as long as you want. For the sake of readability, however, you<br />

may want to break a lengthy instruction into two or more lines. To do so, end the line with a<br />

space followed by an underscore character, and then press Enter and continue the instruction on<br />

the following line. The following code, for example, is a single statement split over three lines:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!