![]() So in search for the variable’s declaration we look for a local scope declaration by that name. Accessibility is how we use code to restrict scope, using keywords like Private, Public, or Friend: if the variable identifier exists in a public module but is declared with the Private keyword, then it’s inaccessible and not in scope for the procedure we’re in. ![]() Scope determines whether that variable identifier is referring to a local, module, or global declaration. ![]() Picture the VBA runtime executing some macro procedure and some variable needs to be incremented by 1. The Global keyword really means Public in VBA, and should be avoided. It also recommends using Public over Global, because nothing is really “global” in VBA and that makes the deprecated keyword potentially confusing. Rubberduck recommends using the Dim keyword only in local scope, and to use the Private keyword to declare module-level variables. The different scopes of VBA: Global, project, module, and local.īecause in VBA class modules are private by default, and a public class is only PublicNotCreatable (as in, a referencing project cannot create a New instance of a class, factory methods must be provided), and also because “ actually global” is in reality slightly more complicated than that (the VB_GlobalNamespace attribute is always going to be False for a VBA class), for the sake of simplicity when I talk about “global scope” and “globals” in this article, I’m treating global and project scopes as one and the same – but it’s important to know the difference, especially more so in scenarios where a VBA/Excel add-in/library is being referenced by other VBA projects, where a tidy public API is handy.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |