ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   option explicit (https://www.excelbanter.com/excel-discussion-misc-queries/108534-option-explicit.html)

Dave F

option explicit
 
Hi,

Two quick questions about 'option explicit.'

1) What does it do?

2) How do I configure VBE to automatically set option explicit?

I know it has something to do with declaring variables, but I don't know why
it's important. Thanks,

Dave
--
Brevity is the soul of wit.

Kevin B

option explicit
 
Option explicit forces the user to declare variables before the variable can
be used.

In the VB Editor (Alt + F11) click TOOLS in the menu and select OPTIONS.
Click the EDITOR tab and then click the REQUIRE VARIABLE DECLARATION check
box to turn OPTION EXPLICIT on.
--
Kevin Backmann


"Dave F" wrote:

Hi,

Two quick questions about 'option explicit.'

1) What does it do?

2) How do I configure VBE to automatically set option explicit?

I know it has something to do with declaring variables, but I don't know why
it's important. Thanks,

Dave
--
Brevity is the soul of wit.


Dave Peterson

option explicit
 
And an example of why it's important.

Ctr_1 = Ctr_l + 1
looks like it should update ctr_1. But the right hand side uses ctr_L (ell, not
one). So if you had forced yourself to delclare your variables with "Option
Explicit", it might be moments of debugging time instead of hours.

And one more reason I like it is that when I declare an object explicitly:

Dim Wks as worksheet

I get VBA's intellisense to help out when I'm writing the code.

wks.
As soon as I hit that dot, I get a list of properties and methods that could
come next.

And one more reason.

If I declare a variable like:

dim myWksThatShowsTheSummary as Worksheet

I can type mywks and hit ctrl space bar to have the VBE autocomplete (or show me
possible matches) that variable. And I know that when I finish coding that
statement, the VBE will match my upper/lower case in the Dim statement.

If the variable stays lower case, I know I made a typo.

Dave F wrote:

Hi,

Two quick questions about 'option explicit.'

1) What does it do?

2) How do I configure VBE to automatically set option explicit?

I know it has something to do with declaring variables, but I don't know why
it's important. Thanks,

Dave
--
Brevity is the soul of wit.


--

Dave Peterson


All times are GMT +1. The time now is 02:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com