Microsoft KB Archive/162971

= XL97: Public Variables Are Not Seen on Modules for Sheet or UserForm =

Article ID: 162971

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q162971



SYMPTOMS
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 INFORMATION
Microsoft 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 Object
The following example declares a public variable in a code module that is associated with an object:  Save and close any open workbooks, and then create a new workbook. Start the Visual Basic Editor (press ALT+F11). In the Project Explorer window, double-click Sheet1.

This step displays the code module associated with Sheet1.  Type the following code in this module: Public gMyVar As Integer

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

gMyVar = gMyVar + 1 MsgBox gMyVar

End Sub  On the Insert menu, click Module.

This step inserts a general module into your project.  Type the following code in this module: Sub General_Module

gMyVar = gMyVar + 1 MsgBox gMyVar

End Sub </li> Press ALT+F11 to switch to Microsoft Excel, and then click Sheet1.</li> Click any cell other than the current cell in Sheet1.

A message box appears with the current value of the variable "gMyVar."</li> Click OK to dismiss the message box.</li> Repeat steps 8 and 9 several times until the value that is displayed in the message box is incremented to 5.</li> Run the General_Module macro.

A message box that displays the value 1 appears. This is the value of gMyVar in the general module. The value is 1 because the General_Module macro can not access the public variable gMyVar that you declared in the code module associated with Sheet1.</li> Select a different cell in Sheet1.

A message box that displays the current value of gMyVar from the code module associated with Sheet1 appears.</li></ol>

Declaring a Public Variable in the General Module
The following example declares a public variable in the general module: <ol> Start the Visual Basic Editor.</li>  Cut the following line Public gMyVar As Integer from the module associated with Sheet1 and paste it into the general declarations section of the general module. </li> Switch to Microsoft Excel and click any cell other than the current cell in Sheet1.

A message box that displays the current value of the variable gMyVar appears.</li> Click OK to dismiss the message box.</li> Repeat steps 3 and 4 several times until the value that is displayed in the message box is incremented to 5.</li> Run the General_Module macro.

A message box that displays a value that is one more than the value that appears in step 5 appears. Because the variable gMyVar is a public variable that is declared on a general module, any module in the project can access its value.</li></ol>

<div class="references_section">