Article ID: 162971 - Last Review: October 11, 2006 - Revision: 2.2 XL97: Public Variables Are Not Seen on Modules for Sheet or UserFormThis article was previously published under Q162971 On This PageSYMPTOMS
If you declare a public variable on a Visual Basic for Applications code
module that is associated with an object, such as a UserForm or a Worksheet
object, only the procedures in the code module in which you declare the
variable can access the value the variable stores.
CAUSE
A variable that you declare with the Public statement in code modules that
are associated with an object, such as a UserForm and Worksheet object, is
available only to the module in which you declare it.
RESOLUTION
If you want to access a public variable in all procedures in all modules of
a project, declare the variable on a general module. To insert a general
module in a project, click Module on the Insert menu.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following examples demonstrate the difference between declaring a
public variable in a general module and declaring a public variable in a
code module that is associated with an object.
Declaring a Public Variable in a Code Module Associated with an ObjectThe following example declares a public variable in a code module that is associated with an object:
Declaring a Public Variable in the General ModuleThe following example declares a public variable in the general module:
REFERENCES
For more information about the scope of variables, click the Office
Assistant in the Visual Basic Editor, type "scope," click Search, and then
click to view "Understanding Scope and Visibility."
NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base: 120802 (http://support.microsoft.com/kb/120802/EN-US/ ) Office: How to Add/Remove a Single Office Program or Component | Article Translations
|

Back to the top
