XL: Deactivate Macro Doesn't Run When You Open HTML Document

ID: Q180699

The information in this article applies to:

SYMPTOMS

When you open a workbook, a macro that is designed to run when you deactivate the active worksheet does not run.

CAUSE

This problem occurs when the following conditions are true:

When you open an HTML document, OnSheetDeactivate and Deactivate macros that are assigned to the active worksheet do not run. However, after you open the HTML document, the OnSheetDeactivate and Deactivate macros work correctly.

NOTE: This problem does not occur when a macro is attached to the Deactivate event of the active workbook.

WORKAROUND

To work around this problem, follow these steps:

1. Click the worksheet that was active when you opened the .htm

   document.

2. Switch to the HTML document.

   The OnSheetDeactivate or Deactivate macro runs correctly.

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/

To create an OnSheetDeactivate macro in Microsoft Excel, follow these steps:

1. On the Tools menu, point to Macro, and then click Visual Basic Editor.

2. On the Insert menu, click Module.

3. Type the following code into the new module:

      Sub Trigger()
         Sheets("Sheet1").OnSheetDeactivate = "RunMe"
      End Sub

      Sub RunMe()
         MsgBox "You deactivated Sheet1."
      End Sub

4. Run the Trigger macro.

When you deactivate Sheet1, the macro specified in the OnSheetDeactivate command ("RunMe") is run. You can specify any worksheet or workbook when you use OnSheetDeactivate; if you specify a workbook, the macro is run when you deactivate any sheet in the workbook.

You can also use the Deactivate event of a worksheet to run a macro. To do this, follow these steps:

1. On the Tools menu, point to Macro, and then click Visual Basic Editor.

2. In the Project window, double-click the sheet for which you want to

   add a macro.

3. In the Object list, click Worksheet. In the Procedure list, click
   Deactivate.

4. Change the code in the Code window to the following:

      Private Sub Worksheet_Deactivate()
         MsgBox "You deactivated Sheet1."
      End Sub

5. On the File menu, click "Close and Return to Microsoft Excel."

When you deactivate Sheet1, the Worksheet_Deactivate macro runs automatically. It is not necessary to use a second macro to set the Worksheet_Deactivate macro to run.

Example

To see an example of this problem, follow these steps:

1. Create a new workbook, and repeat steps 1 through 5 above.

2. On the File menu, click Open. Select an HTML document, and then click

   Open.

   The macro does not run.

3. Switch to the workbook and click Sheet1. Then, switch to the .htm
   document.

   The macro runs as expected.

Additional query words: XL97 XL98 htm .htm .html
Keywords          : kbdta xlloadsave KbVBA xlweb 
Version           : WINDOWS:97; MACINTOSH:98
Platform          : WINDOWS
Issue type        : kbbug

Last Reviewed: May 18, 1999