The information in this article applies to:
SYMPTOMS
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.
CAUSEVisual Basic ProcedureIn 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
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.
MS Excel 4.0 MacroIn 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
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.
WORKAROUNDTo 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. Visual Basic ProcedureUse 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:
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.
STATUSMicrosoft 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. MORE INFORMATION
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.
REFERENCESFor 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 |
Last Reviewed: October 26, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |