Deactivate Macro Doesn't Run When You Open HTML Document (180699)



The information in this article applies to:

  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q180699

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.

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. 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.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbbug KB180699