Microsoft KB Archive/167383

= OFF97: Activate Event for UserForm May Not Be Triggered =

Article ID: 167383

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Word 97 Standard Edition
 * Microsoft PowerPoint 97 Standard Edition

-



This article was previously published under Q167383





SYMPTOMS
If you assign a Microsoft Visual Basic for Applications macro to the Activate event of a UserForm, the macro may not run when you activate the UserForm.



CAUSE
The Activate event for a UserForm may not occur if all of the following conditions are true:
 * You create a macro and assign it to the Deactivate event for one UserForm. -and-


 * You create a macro and assign it to the Activate event for another UserForm. -and-


 * The UserForm with the Deactivate event macro is the active UserForm, and you run a macro that activates the second UserForm with the Activate event macro.



RESOLUTION
To work around this problem, add the Visual Basic for Applications macro code for the Deactivate event for the first UserForm to the macro that activates the second UserForm. When you do this, add the code for the Deactivate event above the code that activates the second UserForm.



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



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.

Example
The following steps demonstrate the behavior described in this article:  Close and save any open workbooks, and then create a new workbook. Start the Visual Basic Editor (press OPTION+F11). On the Insert menu, click UserForm.

This step inserts UserForm1 into the project. Double-click the UserForm to display the Code module behind UserForm1.  Enter the following code for the Deactivate event for the UserForm: Private Sub UserForm_Deactivate

MsgBox "deactivating UserForm1"

End Sub </li> Add a CommandButton to UserForm1.</li>  Enter the following code for the Click event for the CommandButton: Private Sub CommandButton1_Click

UserForm2.Show

End Sub </li> On the Insert menu, click UserForm.

This step inserts UserForm2 into the project.</li> Double-click UserForm2 to display the Code module behind the UserForm.</li>  Enter the following code for the Activate event for the UserForm: Private Sub UserForm_Activate

MsgBox "activating UserForm2"

End Sub </li> Switch to UserForm1, and then press F5 to run UserForm1.</li> Click the CommandButton on UserForm1.

A message box appears with the message "deactivating UserForm1." This confirms that the Deactivate event for UserForm1 occurred.</li> Click OK in the message box.

UserForm2 is the active UserForm, and the message box for the Activate event for UserForm2 does not appear.</li></ol>

Correcting the Example
To correct the example, follow these steps: <ol> Comment the code for the Deactivate event of UserForm1 by typing an apostrophe (') at the beginning of each line of code in the macro.</li>  Change the code for the CommandButton Click event to the following: Private Sub CommandButton1_Click

MsgBox "deactivating UserForm1" UserForm2.Show

End Sub </li> Switch to UserForm1 and press F5 to run the UserForm.

A message box displays the message "deactivating UserForm1."</li> Click OK.

Another message box displays the message "activating UserForm2."</li> Click OK.

UserForm2 is the active UserForm.</li> Close UserForm2, and then close UserForm1.</li></ol>

<div class="references_section">