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?
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?
As long as that variable is dimmed,no.
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
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.
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:
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.Please Login or Register to view this content.
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.
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.
Are there any errors in the other code?
When there is that's the only time I've experienced 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.
I just had this issue occur and am posting my solution in case anyone else has the same issue:
- Cut global declarations
- Try running the code (error thrown on missing global variable)
- Paste global declarations back in
- Run code again, and missing local variables now correctly throwing compile errors
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks