ID: Q126315
5.00 WINDOWS kbprg kbcode xlwin
The information in this article applies to:
In Microsoft Excel, if you use the OnSheetActivate property, or the OnSheetDeactivate property in a Visual Basic for applications, and you do not specify a specific sheet for the property to apply to, the assigned macro may not run when a sheet in Microsoft Excel is activated or deactivated.
If you use the ON.SHEET function in an MS Excel 4.0 macro, and you do not specify a specific sheet for the property or function to apply to, the assigned macro may not run when a sheet in Microsoft Excel is activated or deactivated.
In a Visual Basic procedure, this behavior occurs if you use the OnSheetActivate property and the OnSheetDeactivate property in the same procedure. This behavior only occurs if you do not specify a specific sheet for the function to apply to; that is, you use the property of the Application object. The property that appears last in the procedure behaves properly.
For example, when you run the following procedure
Sub Test()
Application.OnSheetActivate = "Act"
Application.OnSheetDeactivate = "Deact"
End Sub
only the procedure "Deact" runs when you activate another sheet in
Microsoft Excel. Note that in this case, both "Act" and "Deact" should run,
because when you activate another sheet, you are deactivating the
previously active sheet.
In an MS Excel 4.0 macro, this behavior occurs if you use the ON.SHEET function with the activate_logical argument set to TRUE (or omitted) and the ON.SHEET function with the activate_logical argument set to FALSE (to assign a macro to run when a sheet is deactivated), in the same macro. This behavior only occurs if you do not specify a specific sheet for the function to apply to. The function that appears last in the macro behaves properly.
For example, when you run the following macro
A1: =ON.SHEET(,"act")
A2: =ON.SHEET(,"deact",FALSE)
A3: =RETURN()
only the macro "deact" runs when you activate another sheet in Microsoft
Excel. Note that in this case, both "act" and "deact" should run, because
when you activate another sheet, you are deactivating the previously active
sheet.
To work around this behavior, and to assign a specific macro to run when any sheet in any open workbook is activated or deactivated, use the following procedure.
Use the For Each...Next method to loop through each of the sheets in each of the open workbooks and apply the property to each sheet as in the following example:
Sub Test()
Dim osheet As Object, obook As Object
For Each obook In Application.Workbooks
For Each osheet In obook.Sheets
' assign macro 'act' to run when sheet is activated
osheet.OnSheetActivate = "Act"
' assign macro 'deact' to run when sheet is deactivated
osheet.OnSheetDeactivate = "Deact"
Next osheet
Next obook
End Sub
Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are
provided 'as is' and Microsoft does not guarantee that they can be used in
all situations. While Microsoft support professionals can help explain the
functionality of a particular macro, they will not modify these examples to
provide added functionality, nor will they help you construct macros to
meet your specific needs. If you have limited programming experience, you
may want to consult one of the Microsoft Solution Providers. Solution
Providers offer a wide range of fee-based services, including creating
custom macros. For more information about Microsoft Solution Providers,
call Microsoft Customer Information Service at (800) 426-9400.
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Excel version 5.0c.
You can use the OnSheetActivate property in a Visual Basic procedure to set the name of a macro that you want to run when the specified sheet is selected. If you use this property with the Application object, that is, if you do not specify a specific sheet, the assigned macro runs when any sheet in any open workbook is activated.
You can use the ON.SHEET function in an MS Excel 4.0 macro to set the name of a macro that runs whenever the specified sheet is activated from another sheet. If you omit the sheet_text argument when you use this function, the assigned macro runs when any sheet in any open workbook is activated.
For more information about the OnSheetActivate Property, choose the Search button in the Visual Basic Reference and type:
OnSheetActivate Property
For more information about ON.SHEET, choose the Search button in the
Microsoft Excel Macro Functions Help and type:
ON.SHEET function
KBCategory: kbprg kbcode
KBSubcategory: xlwin
Additional reference words: 5.00
Keywords : xlwin
Version : 5.00
Platform : WINDOWS
Last Reviewed: May 17, 1999