XL5: Macro Specified with OnSheetActivate/Deactivate Doesn't Run |
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.
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.
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.
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 PropertyFor more information about ON.SHEET, choose the Search button in the Microsoft Excel Macro Functions Help and type:
ON.SHEET function
Additional query words:
Keywords : xlwin
Version : WINDOWS:5.0
Platform : WINDOWS
Issue type :
Technology :
|
Last Reviewed: October 26, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |