Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Module-Level Variables Are Reset to Their Default Values


View products that this article applies to.

This article was previously published under Q231089

↑ Back to the top


Symptoms

When you run a Microsoft Visual Basic for Applications macro (Sub procedure) for the second time from a workbook, both the public and private module-level variables in your macro are reset to their default settings.

↑ Back to the top


Cause

This problem occurs when you do the following:
  1. You declare one or more variables using the Dim, Private, or Public keyword in the declaration section of a standard module.

    -and-
  2. You write a Sub procedure in the same module and use the module-level variables.

    -and-
  3. You click the Design Mode button on the Standard toolbar of the Visual Basic Editor, or on the Control Toolbox in Excel.

    -and-
  4. You run the macro a second time.

↑ Back to the top


Workaround

To work around this problem, do not go into design mode until you no longer need to run your code.

↑ Back to the top


References

For more information about understanding the scope and visibility of variables, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type understanding scope and visibility in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about the lifetime of variables, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type understanding the lifetime of variables in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about how to declare a variable, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type declaring variables in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB231089, kbnofix, kbdtacode, kbbug

↑ Back to the top

Article Info
Article ID : 231089
Revision : 5
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 224