© Roy Cox (2005 - 2020)

.

Why I use Option Explicit

Page Title

Making Excel work for you.
Option Explicit is a Statement placed at the top of the code module before any Procedures which forces you to explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name when you try to run your code it will be highlighted, and you will get a clear error, "Variable not defined" One advantage of using Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear. The more code you have the harder it gets to find errors - generally all you know is that the code is not working properly, but you can't see why. Instead of spending lots of your time trying to work it out, simply placing Option Explicit at the top of the code module will tell you what and where the problem is. Once highlighted then all you need to do is correct the variable name or declare the variable if it missing. You can set the VB Editor to automatically add this statement to all new code modules that you create. You will need to add it manually to existing files. If you think the advantages of using Option Explicit , which I do, are worth using it then you can set Excel to add Option Explicit automatically to all new modules, including UserForms, that you create. Simply open the VB Editor and select Tools -> Options, and tick the "Require Variable Declaration" box.
If you find this article useful You can help maintain my site by donating. Simply click the image below
Buy me a coffeeBuy me a coffee