XL97: Public Variables Are Not Seen on Modules for Sheet or UserForm (162971)



The information in this article applies to:

  • Microsoft Excel 97 for Windows

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:
  1. Save and close any open workbooks, and then create a new workbook.
  2. Start the Visual Basic Editor (press ALT+F11).
  3. In the Project Explorer window, double-click Sheet1.

    This step displays the code module associated with Sheet1.
  4. 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
    						
  5. On the Insert menu, click Module.

    This step inserts a general module into your project.
  6. Type the following code in this module:
            Sub General_Module()
    
               gMyVar = gMyVar + 1
               MsgBox gMyVar
    
            End Sub
    						
  7. Press ALT+F11 to switch to Microsoft Excel, and then click Sheet1.
  8. Click any cell other than the current cell in Sheet1.

    A message box appears with the current value of the variable "gMyVar."
  9. Click OK to dismiss the message box.
  10. Repeat steps 8 and 9 several times until the value that is displayed in the message box is incremented to 5.
  11. 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.
  12. Select a different cell in Sheet1.

    A message box that displays the current value of gMyVar from the code module associated with Sheet1 appears.

Declaring a Public Variable in the General Module

The following example declares a public variable in the general module:
  1. Start the Visual Basic Editor.
  2. 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.
  3. 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.
  4. Click OK to dismiss the message box.
  5. Repeat steps 3 and 4 several times until the value that is displayed in the message box is incremented to 5.
  6. 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.

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 Office: How to Add/Remove a Single Office Program or Component

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbProgramming KB162971