PDA

View Full Version : [SOLVED:] When is a Global Variable not Global?



MichaelH
02-08-2005, 02:50 AM
Hi All,

This is a follow up to a previous puzzler but is likely of interest to others.

In a new project I created a module called GlobalVars into which I declared, as Global, all the variables that would have been repeated in all the other modules - variables to trap error codes, MsgBox responses etc.

In the Workbook module, within the Before Open event procedure, I referred to two of the above global variables; but one came up at run time as variable not defined. I 100 X checked the spelling but still don't know why one global variable was OK but not another.
The failing variable was named REPLY, in the end I changed it to MsgReply and that worked. I don't think REPLY is a reserved word is it?

Is this a good use of Global variables, I did it this way to save defing the same use of variables many times.

Thanks

Michael
PS the global variable REPLY is referenced in other modules within the project OK, it's only within the Workbook module that this error occurs.

Andy Pope
02-08-2005, 03:00 AM
Hi Michael,

If you are not already I suggest you use Option Explicit, as this would have brought your attention to a problem with the use of Reply.

And then from the help file I found this as the reason.


Reply Method
You have requested Help for a Visual Basic keyword used only on the Macintosh. For information about this keyword, consult the language reference Help included with Microsoft Office Macintosh Edition.

MichaelH
02-08-2005, 05:04 AM
Andy,

Many thanks,
I'm not sure why a Mac keyword affects in Windows and why the variable "REPLY" is Ok in other modules than the Workbook module - even using F1 on the Help text didn't refer to "REPLY" as a keyword - where did you get to your text please?

All I could find was "ReplyWithChanges" when I searched further.

I do use Option Explicit (that's what we were taught by Jacob, lesson 1), it's set up in Options, but I've just noticed that it doesn't appear in any of the procedures in the Workbook module so I didn't spot it, presumably that's the one place you have to hard-code it yourself?

However, I've just added it into my BeforeClose procedure and now it tells me that a different variable ("AddinOK") is not defined, that is also declared as Global, but it was fine before surely that's not a keyword!

This is not clear or definitive.

Cheers
Michael

Help Text from Help on Error Message box:-
Expected Function or variable





The syntax of your statement indicates a variable (javascript:hhobj_3.Click()) or function call. This error has the following cause and solution:
<LI class=LB1>The name isn't that of a known variable or Function procedure. Check the spelling of the name. Make sure that any variable or function with that name is visible in the portion of the program from which you are referencing it. For example, if a function is defined as Private or a variable isn't defined as Public, it's only visible within its own module (javascript:hhobj_4.Click()).

You are trying to inappropriately assign a value to a procedure (javascript:hhobj_5.Click()) name. For example if MySub is a Sub procedure, the following code generates this error: MySub = 237 ' Causes Expected Function or variable errorAlthough you can use assignment syntax with a Property Let procedure or with a Function that returns an object or a Variant containing an object, you can't use assignment syntax with a Sub, Property Get, or Property Set procedure.

For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).

Andy Pope
02-08-2005, 05:42 AM
Hi,

The word is probably reserved inorder to maintain consistancey if not functionality between Windows and Mac versions.

To get the help I simply highlighted the word and pressed F1. Worked for both xl2k and xl2003.

The 'Require Variable Declaration' option, for me at least, puts Option Explicit at the top of all modules and classes. Including sheets and Thisworkbook.

The one thing I can't explain is the exceptance of Reply in all places execpt Thisworkbook.

Another useful coding suggestion is the use of a prefixed naming convention.
This would normally keep you away from reserved words.

MichaelH
02-08-2005, 06:11 AM
Andy,

Good advice, thank you.
I'm going to set this as solved as life is full enough without solving all inconsistencies.

I just created a new s/s and went to the BeforeClose event in ThisWorkbook and the Option Explicit is there, I must have deleted it in my previous project by mistake.

I'm using Excel XP (2002) under WinXP so maybe there is a difference there than what you see.


ttfn
Michael :)