[vba] What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?

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?

This question is related to vba vb6 scope access-modifiers

The answer is


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.


Examples related to vba

Copy filtered data to another sheet using VBA Better way to find last used row Check if a value is in an array or not with Excel VBA Creating an Array from a Range in VBA Excel: macro to export worksheet as CSV file without leaving my current Excel sheet VBA: Convert Text to Number What's the difference between "end" and "exit sub" in VBA? Rename Excel Sheet with VBA Macro Extract Data from PDF and Add to Worksheet Quicker way to get all unique values of a column in VBA?

Examples related to vb6

Add newline to VBA or Visual Basic 6 Visual Studio 6 Windows Common Controls 6.0 (sp6) Windows 7, 64 bit ReDim Preserve to a Multi-Dimensional Array in Visual Basic 6 Object Library Not Registered When Adding Windows Common Controls 6.0 VB6 IDE cannot load MSCOMCTL.OCX after update KB 2687323 VBA Convert String to Date Installation of VB6 on Windows 7 / 8 / 10 Escape double quote in VB string How do I resolve "Run-time error '429': ActiveX component can't create object"? What is the difference between dim and set in vba

Examples related to scope

Angular 2 - Using 'this' inside setTimeout Why Is `Export Default Const` invalid? How do I access previous promise results in a .then() chain? Problems with local variable scope. How to solve it? Why is it OK to return a 'vector' from a function? Uncaught TypeError: Cannot read property 'length' of undefined Setting dynamic scope variables in AngularJs - scope.<some_string> How to remove elements/nodes from angular.js array Limiting number of displayed results when using ngRepeat A variable modified inside a while loop is not remembered

Examples related to access-modifiers

What is the default access modifier in Java? What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers? Internal vs. Private Access Modifiers Class is inaccessible due to its protection level What are the default access modifiers in C#? What is the equivalent of Java's final in C#? What are public, private and protected in object oriented programming? In C#, what is the difference between public, private, protected, and having no access modifier? What is the difference between 'protected' and 'protected internal'? What is the difference between public, protected, package-private and private in Java?