Meaning of Option Explicit

  • Actually...I have one question real quick...


    What does "Option Explicit" mean? What does it do/why is it used?

    :rock: Excel rocks...except when it won't read my mind... :guitar:

  • Re: Run-time Error Every Other Time The Macro Runs


    It a nut shell...it forces you to declare all of your variables. Its not necessary, but I reccomend it. It helps if you make a simple error...will trap it for you.

  • Re: Run-time Error Every Other Time The Macro Runs


    Quote from gmccreedy

    It a nut shell...it forces you to declare all of your variables. Its not necessary, but I reccomend it. It helps if you make a simple error...will trap it for you.


    I'll disagree happily with GMc! It is a requirement. It is a real good development aid.


    To expand on what he said,


    Options Explicit requires that you declare using the Dim statement any variables that you use.


    Going one step further, when you declare your variables you should also state their type. This includes function modules and subroutine parameters. So this code is bad:


    Code
    Sub My_Sub(Arg1, Arg2)
    
    
    Function My_Func(Arg1, Arg2)
    
    
    Dim My_Var


    This code is good:


    Code
    Sub My_Sub(Arg1 as String, Arg2 as Long)
    
    
    Function My_Func(Arg1 as Integer, Arg2 as User_Type)
    
    
    Dim My_Var as Long


    The reasons for this:


    Option Explicit


    [INDENT]Option explicit makes you declare all the variables that you use. This prevents bugs from entering your code caused by typo's in variable names. For example accidentally typing My_Var0 rather than My_VarO would be caught by this[/INDENT]


    Typing your variables


    [INDENT]Typing your variables has a few effects. From the development side it prevents errors because the arguments you call a subroutine with must exactly match the formal parameters.


    It increases the speed at which your macros run. When VBA calls a subroutine, if the variables are untyped, it must determine the type of variables the subroutine requires and then coerce the actual arguments to those types.


    Similarly when you use intrinsic functions, if you use a variable that is not typed or not typed correctly, the system has to again coerce the parameter to the formal parameters type.


    If you do type all your variables and parameters, then these two steps do not occur, resulting in less time being spent doing this chore by the macro. This does not have much of an effect in straight code, but imagine a 10,000 iteration loop having to do these conversions all the time.
    [/INDENT]


    Regards


    Rich

  • Re: Run-time Error Every Other Time The Macro Runs


    Rich, I agree with most of what you say. But Option Explicit is not a "requirement".


    Quote

    Option explicit makes you declare all the variables that you use. This prevents bugs from entering your code caused by typo's in variable names. For example accidentally typing My_Var0 rather than My_VarO would be caught by this

    That isn't the use of Option Explicit, simply the fact you have already typed the variable name with Dim.



    While it does force variable declaration (when the Option is checked), it doesn't force the use of the right type and meaningful naming conventions. I detest the use of x, y etc :)

  • Re: Run-time Error Every Other Time The Macro Runs


    Quote from Dave Hawley

    Rich, I agree with most of what you say. But Option Explicit is not a "requirement".



    I agree with you in that it is an optional statement in VBA and programs will work without it.

    However, it's a starting point to good programming practices in that it makes you declare your variables to start off with. Alright, you can be lazy and just declare them as a default variant, but you still have to declare them.

    So, for me, anything that promotes good programming practice is a "requirement".

    Quote from Dave

    That isn't the use of Option Explicit, simply the fact you have already typed the variable name with Dim.



    Yes, but without Option Explicit, that code would not be noticed and you would be assigning the value of My_Var_zero to My_Var_Oscar which is not what you mean to do. Option Explicit would cause an undefined variable at My_Var_Oh (assuming that you've declared the _zero version). So yes, the example is valid for Option Explicit.

    Regards

    Rich

  • Re: Run-time Error Every Other Time The Macro Runs


    Quote

    However, it's a starting point to good programming practices in that it makes you declare your variables to start off with. Alright, you can be lazy and just declare them as a default variant, but you still have to declare them.


    So, for me, anything that promotes good programming practice is a "requirement".

    Agree it's good pratice, just not that it's a "requirement". It it were, VBA code would not run without it.

  • Re: Run-time Error Every Other Time The Macro Runs


    Quote from Dave Hawley

    Agree it's good pratice, just not that it's a "requirement". It it were, VBA code would not run without it.


    <grins>

  • Re: Run-time Error Every Other Time The Macro Runs


    Another word of advice that I can't see has been raised yet - for 'Option Explicit' to be fully effective it must be used at the top of each code module in which code appears. To ensure that this happens automatically, go to the VBE Tools > Options > Editor tab and ensure that 'Require Variable Declaration' is checked.

    [COLOR="Purple"]Regards,[/COLOR]
    [COLOR="Purple"]Batman.[/COLOR]

  • Re: Meaning of Option Explicit


    Hi,
    If u declare Option explicit, then you must delclare the variables with proper type, else it will throw on error. This option used to reduce no of bugs in our project. This is not optional but if you use this option product quality will be good.

  • Re: Meaning of Option Explicit


    It doesn't force the use of correct type, only that you declare any variables. Passing the wrong type to a variable will result in an error with/without Option Explicit

  • Re: Meaning of Option Explicit


    Quote from Dave Hawley

    It doesn't force the use of correct type, only that you declare any variables. Passing the wrong type to a variable will result in an error with/without Option Explicit


    Not if what you pass is a valid variant. It'll only error when you try and use it in a situation where something requires a type:


    Code
    Sub Test()
        A = 1.2
        Debug.Print Sin(A)
        A = "34.35"
        Debug.Print Sin(A)
        A = "String"
        Debug.Print Sin(A)
    End Sub


    Will only error on the third debug.print. However, declare A as double and it falls over when you try and assign "String" to A. Not much difference in the small example above, but in a real world example assigning a string to a variable that is supposed to be numeric might cause an error that is many lines away from where the actual erro (ie the assignment) is.


    Regards


    Rich

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!