XL97: Public Variables Are Not Seen on Modules for Sheet or UserFormID: Q162971
|
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.
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.
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.
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals 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 needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.aspThe 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.
Public gMyVar As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
gMyVar = gMyVar + 1
MsgBox gMyVar
End Sub
Sub General_Module()
gMyVar = gMyVar + 1
MsgBox gMyVar
End Sub
Public gMyVar As Integer
from the module associated with Sheet1 and paste it into the general
declarations section of the general module.
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:
Q120802
Office: How to Add/Remove a Single Office
Program or Component
Additional query words: 97 XL97
Keywords : kbprg kbdta
Version : WINDOWS:97
Platform : WINDOWS
Issue type :
Last Reviewed: July 12, 1999