0% found this document useful (0 votes)
20 views

VBA Tips & Tricks

Option Explicit forces you to declare all the variables you use. The value of a variable declared as static is retained after a variable ends. You can declare constants by using the Const statement.

Uploaded by

Rahul Bhatt
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

VBA Tips & Tricks

Option Explicit forces you to declare all the variables you use. The value of a variable declared as static is retained after a variable ends. You can declare constants by using the Const statement.

Uploaded by

Rahul Bhatt
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 2

To force yourself to declare all the variables you use, include the following as the first statement in your

VBA module: Option Explicit When this statement is present, you wont be able to run your code if it contains any undeclared variable. You need to use Option Explicit only once: at the beginning of your module, prior to the declaration of any procedures in the module. Keep in mind that the Option Explicit statement applies only to the module in which it resides. If you have more than one VBA module in a project, you need an Option Explicit statement for each module. Dont wear yourself out typing the entire name of a variable. Just type the first two or three characters and then hit Control+Space. The VBE will either complete the entry for you or if the choice is ambiguous show you a pick list to select from. In fact, this slick trick works with reserved words too. Even though the value of a variable declared as Static is retained after a variable ends, that variable is unavailable to other procedures. In the preceding MySub procedure example, the Counter variable and its value are available only within the MySub procedure. In other words, its a procedure-level variable. A static variable may be useful if you need to track the number of times you execute a procedure. You can declare a static variable and increment it each time you run the procedure. The code keeps track of the number of times the procedure was executed. The value of the Counter variable is not reset when the procedure ends. But it is reset when you close and reopen the workbook.

you declare constants by using the Const statement:

VBAs InputBox function always returns a string, so if you need to get a value, youll need to do some additional checking. The following example uses the InputBox function to get a number. It uses the IsNumeric function to check whether the string is a number. If the string does contain a number, all is fine. If the users entry cannot be interpreted as a number, the code displays a message box.

To ensure that the Option Explicit statement is inserted automatically whenever you insert a new VBA module, turn on the Require Variable Definition option. You find it in the Editor tab of the Options dialog box (in the VBE, choose ToolsOptions). I highly recommend doing so.

You might also like