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

Last reviewed: February 11, 1998
Article ID: Q180699
The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

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:

  • An OnSheetDeactivate macro is assigned to the active worksheet or workbook, or a macro is attached to the Deactivate event of the active worksheet.

        -and-
    
  • You open a Hypertext Markup Language (HTML) document in Microsoft Excel.

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. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

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 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 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/supportnet/refguide/default.asp

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 : xlvbainfo xlloadsave xlweb
Version : WINDOWS:97; MACINTOSH:98
Platform : WINDOWS
Issue type : kbbug
Solution Type : kbworkaround


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 11, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.