It also helps you avoid confusion when the scope of a variable may not be clear.
To turn Option Explicit on, do the following:
1. From any Excel workbook, open the VBE.
2. From any open workbook, choose the Visual Basic command from the Code tab of the
Developer ribbon, or press Alt+F11.
3. In the VBE, choose Tools ?¤ Options to display the Options dialog box, as shown in
Figure 7-3.
4. If it??™s not already checked, in the Code Settings section in the top half of the Options
dialog box, click the Require Variable Declaration check box. In Figure 7-3, the arrow
cursor points to this item.
Figure 7-3. The VBE??™s Options dialog box
5. Click OK to save the changes and close the Options dialog box.
Once this is done, any time you create a new code module or open the code module
attached to any worksheet, the Option Explicit command will already be inserted at the top
of the module.
Quick Debugging
The VBE provides us with a couple of easy-to-use tools for simple debugging needs. There will
be times where running code through the debugger may be more than you need. In those
cases, here are the tools available to us:
Message boxes: These are used to display information about the code while it??™s running.
The Debug object: The Debug object hosts two methods that will ease our debugging efforts
while code is running as well. These are Print and Assert.
Displaying Information with Message Boxes
Message boxes are useful tools for simple debugging. They allow us to display the values of
variables inside our code while the code is running, and they interrupt the running of the
code.
Pages:
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243