By Ben McCormack

2010-09-28 17:44:40 8 Comments

In VB6/VBA, you can declare module-level variables outside of a specific Sub or Function method. I've used Private and Public before inside modules and understand them like so:

  • Public - visible to all code inside the module and all code outside the module, essentially making it global.
  • Private - visible only to code inside the module.

I've noticed that you can use Dim and Global as modifiers for modular variables. Are Dim and Global different from Private and Public, respectively, when used as access modifiers on modular fields? If so, how are they different?


@Joe Jordan 2010-09-28 18:13:13

Dim and Private work the same, though the common convention is to use Private at the module level, and Dim at the Sub/Function level. 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.

@Ben McCormack 2010-09-28 18:32:59

Thanks for your answer. Your explanation is pretty much what I had been thinking. I'm starting at a huge block of legacy code that has all four modifiers used in the declarations sections of a single module.

@Kiril 2015-03-14 16:10:35

Global variables keep theit value after execution.

@David A. Gray 2015-09-19 22:10:06

Comparing your comments against the online help for Excel 2010 reveals a couple of very fine points: "Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside. Moreover, caution: The Public statement can't be used in a class module to declare a fixed-length string variable. Since both of the above are obscure use cases, neither is likely to be a practical concern. Nevertheless, in the interest of completeness.

@Egalth 2018-10-11 10:41:17

Although the question only concerned module-level declarations, for the sake of completeness (and for newbies) it's worth mentioning explicitly that Private and Public are not even allowed inside procedures. The official documentation of the Private statement seems to have a typo in this regard (it's almost identical to and probably(?) copied from the documentation for the Dim statement): " When you use the Private statement in [sic!] a procedure..."

Related Questions

Sponsored Content

28 Answered Questions

4 Answered Questions

[SOLVED] Variable scopes in Python classes

  • 2011-04-17 01:37:01
  • jason
  • 60597 View
  • 56 Score
  • 4 Answer
  • Tags:   python oop scope

16 Answered Questions

7 Answered Questions

[SOLVED] Internal vs. Private Access Modifiers

8 Answered Questions

7 Answered Questions

[SOLVED] "Private" (implementation) class in Python

9 Answered Questions

[SOLVED] What are the default access modifiers in C#?

  • 2010-03-26 06:28:09
  • Surya sasidhar
  • 234380 View
  • 381 Score
  • 9 Answer
  • Tags:   c# access-modifiers

16 Answered Questions

[SOLVED] Are there any reasons to use private properties in C#?

2 Answered Questions

[SOLVED] Public vs Private/Dim in Excel VBA

5 Answered Questions

Sponsored Content