Microsoft KB Archive/188441

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

Article ID: 188441

Article Last Modified on 6/17/2005

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q188441



SUMMARY
If you declare a public variable in a Visual Basic for Applications module that is associated with an object, such as a UserForm or a Worksheet object, only the procedures in the module can access the value of the variable.

If you declare a variable with the Public statement in a module that is associated with an object, such as a UserForm and Worksheet object, the variable is only available in that module.

If you want to access a public variable in all procedures for all modules of a project, you must declare the variable in 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 module that is associated with an object.

Declaring a Public Variable in a Module Associated with an Object
To declare a public variable in a module that is associated with an object, follow these steps:

 Save and close any open workbooks, open a new workbook, and start the Visual Basic Editor by pressing OPTION+F11. In the Project Explorer window, double-click Sheet1.

NOTE: This step displays the module associated with Sheet1.  Type the following code in the 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. NOTE: 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  Switch to Microsoft Excel by pressing OPTION+F11, and click somewhere else in Sheet1 to remove focus from the current cell.

NOTE: A message box appears with the current value of the variable "gMyVar." Close the message box by clicking OK.</li> Repeat step 6 and step 7 several times until the value displayed in the message box is 5.</li> Run the General_Module macro.

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

A message box displays the current value of gMyVar from the module associated with Sheet1.</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 by pressing OPTION+F11.</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 somewhere else in Sheet1 to remove focus from the current cell.

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

A message box displays a value that is equal to the value of gMyVar in Step 5 incremented by 1. 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">