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.

642<br />

Part VI: Developing Custom Worksheet Functions<br />

Forcing yourself to declare all variables<br />

To force yourself to declare all the variables that you use, include the following as the first<br />

instruction in your VBA module:<br />

Option Explicit<br />

This statement causes your procedure to stop whenever VBA encounters an undeclared variable<br />

name. VBA issues an error message (Compile error: Variable not defined), and you<br />

must declare the variable before you can proceed.<br />

To ensure that the Option Explicit statement appears in every new VBA module automatically,<br />

enable the Require Variable Declaration option on the Editor tab of the VB Editor Options<br />

dialog box. To display this dialog box, choose Tools➜Options.<br />

The second statement declares two constants with a single statement, but it does not declare a<br />

data type. Consequently, the two constants are variants. Because a constant never changes its<br />

value, you normally want to declare your constants as a specific data type. The scope of a constant<br />

depends on where it is declared within your module:<br />

To make a constant available within a single procedure only, declare it after the Sub or<br />

Function statement to make it a local constant.<br />

To make a constant available to all procedures in a module, declare it before the first procedure<br />

in the module.<br />

To make a constant available to all modules in the workbook, use the Public keyword<br />

and declare the constant before the first procedure in a module. The following statement<br />

creates a constant that is valid in all VBA modules in the workbook:<br />

Public Const AppName As String = “Budget Tools”<br />

If you attempt to change the value of a constant in a VBA procedure, you get an error —<br />

as you would expect. A constant is a constant, not a variable.<br />

Using constants throughout your code in place of hard-coded values or strings is an excellent<br />

programming practice. For example, if your procedure needs to refer to a specific value (such as<br />

an interest rate) several times, it’s better to declare the value as a constant and use the constant’s<br />

name rather than its value in your expressions. This technique makes your code more readable<br />

and makes it easier to change should the need arise — you have to change only one instruction<br />

rather than several.<br />

VBA and Excel define many constants that you can use in your code without declaring them. For<br />

example, the following statement uses a constant named vbInformation:<br />

MsgBox “Hello”, vbInformation

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

Saved successfully!

Ooh no, something went wrong!