I have some constants declared in my VBA code as:
global const variable = value
These declarations are working properly, by which I mean they don't cause an error and the constants are used in other modules without error.
In reviewing the scope of my variables, I've looked at the Microsoft documentation to ensure that I'm doing it right. But lo and behold, I find that Microsoft doesn't even define global
as a keyword! The official list of keywords for VBA does not include global
:
Furthermore, the syntax for the const
statement, given at:
is:
[ Public | Private ] Const constname [ As type ] = expression
which does not allow global
as a modifier. This should mean that my declarations should be throwing a compiler error.
Moreover, three Microsoft pages about scope in VBA do not include the word "global" anywhere in their text:
- https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/scope
- https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-scope-and-visibility
- https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-the-lifetime-of-variables
I found one page of Microsoft documentation that refers to a keyword global
, which is in the context of defining namespaces:
- https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/namespaces
Because of that, the list of VBA keywords linked above is wrong for not including global
.
There is a Stack Overflow answer that states, "Public
and Global
are nearly identical in their function, however Global
can only be used in standard modules, whereas Public
can be used in all contexts (modules, classes, controls, forms etc.) Global
comes from older versions of VB and was likely kept for backwards compatibility, but has been wholly superseded by Public
.":
- What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers? (answer)
And there is another Stack Overflow answer that describes the keyword global
as "now deprecated, but still valid":
If those answers are correct, then I should change my code to replace global
with public
. But is there some documentation of the deprecation of global
and the fact that it actually does still remain as a working keyword?