OFF97: Activate Event for UserForm May Not Be Triggered

ID: Q167383

The information in this article applies to:

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:

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

Example

The following steps demonstrate the behavior described in this article:

   1. Close and save any open workbooks, and then create a new workbook.

    2. Start the Visual Basic Editor (press OPTION+F11).

    3. On the Insert menu, click UserForm.

       This step inserts UserForm1 into the project.

    4. Double-click the UserForm to display the Code module behind
       UserForm1.

    5. Enter the following code for the Deactivate event for the UserForm:

          Private Sub UserForm_Deactivate()

             MsgBox "deactivating UserForm1"

          End Sub

    6. Add a CommandButton to UserForm1.

    7. Enter the following code for the Click event for the CommandButton:

          Private Sub CommandButton1_Click()

             UserForm2.Show

          End Sub

    8. On the Insert menu, click UserForm.

       This step inserts UserForm2 into the project.

    9. Double-click UserForm2 to display the Code module behind the
       UserForm.

   10. Enter the following code for the Activate event for the UserForm:

          Private Sub UserForm_Activate()

             MsgBox "activating UserForm2"

          End Sub

   11. Switch to UserForm1, and then press F5 to run UserForm1.

   12. Click the CommandButton on UserForm1.

       A message box appears with the message "deactivating UserForm1."
       This confirms that the Deactivate event for UserForm1 occurred.

   13. Click OK in the message box.

       UserForm2 is the active UserForm, and the message box for the
       Activate event for UserForm2 does not appear.

Correcting the Example

To correct the example, follow these steps:

   1. Comment the code for the Deactivate event of UserForm1 by typing an
      apostrophe (') at the beginning of each line of code in the macro.

   2. Change the code for the CommandButton Click event to the following:

         Private Sub CommandButton1_Click()

             MsgBox "deactivating UserForm1"
             UserForm2.Show

          End Sub

   3. Switch to UserForm1 and press F5 to run the UserForm.

      A message box displays the message "deactivating UserForm1."

   4. Click OK.

      Another message box displays the message "activating UserForm2."

   5. Click OK.

      UserForm2 is the active UserForm.

   6. Close UserForm2, and then close UserForm1.

REFERENCES

For more information about using the Activate and Deactivate events, click the Office Assistant while in the Visual Basic Editor, type "activate, deactivate" (without the quotation marks), click Search, and then click to view "Activate, Deactivate Events."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176476
   TITLE     : OFF: Office Assistant Not Answering Visual Basic Questions

Additional query words: XL97 ppt97 wd97
Keywords          : kbprg kbdta kbdtacode OffVBA kbhowto 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: April 7, 1999