Microsoft KB Archive/177834

= Public Variables May Be Lost When You Copy a Worksheet =

Article ID: 177834

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q177834



SYMPTOMS
In the versions of Microsoft Excel listed at the beginning of this article, public variables may unexpectedly lose their values. As a result, when you run a Microsoft Visual Basic for Applications procedure or macro that uses the public variables, the procedure or macro may not run correctly or may return incorrect results. (See the "More Information" section of this article for a demonstration of this problem.)



CAUSE
This problem may occur when the following conditions are true:  In the Visual Basic Editor, the option to require variable declarations has been enabled by performing the following steps:

 On the Tools menu, click Options. Click the Editor tab. Select the Require Variable Declaration check box. Click OK.

-and-

  You copied a worksheet, either by clicking Move Or Copy Sheet on the Edit menu or by running a Visual Basic procedure or macro that contains a line of code similar to the following: Worksheets("Sheet1").Copy After:=Worksheets(Worksheets.Count) Or, you moved or copied a worksheet from another workbook into the workbook that contains the public variables. </li></ul>

<div class="resolution_section">

RESOLUTION
To prevent this problem from occurring, follow these steps:


 * 1) In the Visual Basic Editor, click Options on the Tools menu.
 * 2) Click the Editor tab.
 * 3) Click to clear the Require Variable Declaration check box.
 * 4) Click OK.

After completing these steps, your public variables should retain their values even when you copy a worksheet.

<div class="status_section">

STATUS
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.

<div class="moreinformation_section">

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. In Visual Basic for Applications, you can create a public variable by adding the word "Public" to a variable declaration at the top of a module.

Example: Public X As Integer Public Y As String

Sub Test ' <Your code goes here.> End Sub Normally, public variables retain their values until you close the workbook in which they are contained. However, if the conditions listed in the "Cause" section of this article are true, the public variables may unexpectedly lose their values. The following steps demonstrate this problem.

Steps to Reproduce Behavior
 In Microsoft Excel, create a new workbook.</li> On the Tools menu, point to Macro, and then click Visual Basic Editor. On the Insert menu, click Module.</li>  Type the following code into the new module: Public X As Integer Public Y As String

Sub CopySheet Worksheets("Sheet1").Copy After:=Worksheets(Worksheets.Count) End Sub

Sub SetVars X = 5 Y = "Hello!" End Sub

Sub ShowVars MsgBox X         MsgBox Y      End Sub </li> On the Tools menu, click Options. Click the Editor tab. Select the Require Variable Declaration check box, and then click OK.</li> On the File menu, click "Close and Return to Microsoft Excel."</li> On the Tools menu, point to Macro, and then click Macros. Select SetVars, and then click Run.

The variables now contain the values 5 and "Hello!"</li> On the Tools menu, point to Macro, and then click Macros. Select ShowVars, and then click Run. Two message boxes appear containing the values 5 and "Hello!"</li> On the Tools menu, point to Macro, and then click Macros. Select CopySheet, and then click Run.

Or, click Move Or Copy Sheet on the Edit menu. Select the Create A Copy check box. In the list, click "(move to end)", and then click OK.

The worksheet Sheet1 is copied; the new worksheet, "Sheet1 (2)," appears at the end of the workbook.</li> On the Tools menu, point to Macro, and then click Macros. Select ShowVars, and then click Run.

As before, two message boxes appear. However, the first message box contains the value zero (0), and the second message box is empty. This occurs because the act of copying the worksheet reinitialized the public variables.

If you repeat these steps, but leave the Require Variable Declaration check box cleared in step 4, the public variables will correctly retain their values.</li></ol>

<div class="references_section">