+ Reply to Thread
Results 1 to 11 of 11

Option Explicit Not Working

  1. #1
    Registered User
    Join Date
    01-30-2013
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2016
    Posts
    23

    Option Explicit Not Working

    It seems though the Option Explicit option does not always work as advertised. Is there any reason VBA would not flag an undeclared variable when Option Explicit is used in the module?

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Option Explicit Not Working

    As long as that variable is dimmed,no.

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Option Explicit Not Working

    does the project compile? what is the name of the variable?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Option Explicit Not Working

    I've seen that happen, or appear to happen, when there are other errors in the code.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    01-30-2013
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2016
    Posts
    23

    Re: Option Explicit Not Working

    Code compiles no errors. But I think I may know the cause of the latest issue. I have a global ENUM defined similar to this:

    Please Login or Register  to view this content.
    The sub uses the same variable name but normally, you should have to reference it as Data.var1 not just var1 so VBA is not generating a compile error in MySub. But again, this time the code does not generate a compile error so I am not entirely sure what is going on.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Option Explicit Not Working

    I don't get an undeclared error with that code, I get 'Assignment to constant not permitted'.

    That's because var1 and var2 are constants.

  7. #7
    Registered User
    Join Date
    01-30-2013
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2016
    Posts
    23

    Re: Option Explicit Not Working

    I know. I am saying is that sometimes when I used similar code in the complete macro, it doesn't generate the error but when removed into separate macro, it does generate the error. So just by moving it, something changes.

    Also, I noticed that if I dimension the variable and then remove the Dim statement, the error is then generated when before without the Dim statement there was no error. So it appears that VBA is losing track of what is dimensioned and what is not.

    One more odd thing about these modules. When I change something, I get a message asking if it is ok to reset the module. Not sure why that is but it may be related to the above issue. On smaller workbooks/macros this is not an issue but the code and workbook are quite large.
    Last edited by markinpt; 02-05-2013 at 07:13 PM.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Option Explicit Not Working

    Are there any errors in the other code?

    When there is that's the only time I've experienced Option Explicit not working.

  9. #9
    Registered User
    Join Date
    01-30-2013
    Location
    Pleasanton, CA
    MS-Off Ver
    Excel 2016
    Posts
    23

    Re: Option Explicit Not Working

    So the workbook has 10 separate modules with several functions/subs each. When I compile, it compiles everything at once and there are no errors. But once dimensioned and then removed, the error surfaces. The problem is catching the undeclared variable in the first place. Next time it happens, I will have to take note if it is always an ENUM variable with the same name or if other conditions occur as well.

  10. #10
    Registered User
    Join Date
    01-19-2018
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2016
    Posts
    1

    Re: Option Explicit Not Working

    I just had this issue occur and am posting my solution in case anyone else has the same issue:
    1. Cut global declarations
    2. Try running the code (error thrown on missing global variable)
    3. Paste global declarations back in
    4. Run code again, and missing local variables now correctly throwing compile errors

  11. #11
    Registered User
    Join Date
    09-17-2019
    Location
    Madrid
    MS-Off Ver
    Office 365 version 1902
    Posts
    1

    Re: Option Explicit Not Working

    Quote Originally Posted by Michael Lodge View Post
    I just had this issue occur and am posting my solution in case anyone else has the same issue:
    1. Cut global declarations
    2. Try running the code (error thrown on missing global variable)
    3. Paste global declarations back in
    4. Run code again, and missing local variables now correctly throwing compile errors
    Thanks a lot. This worked for me!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1