The information in this article applies to:
SUMMARYIn Microsoft Excel, you can use the SheetChange event in Microsoft Visual Basic for Applications to automatically run a macro whenever you change the contents of any worksheet in the workbook. This article explains how to use the SheetChange event and the SheetChange subroutine. MORE INFORMATIONMicrosoft 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 professionals 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 a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.asp How to Access the SheetChange Subroutine in the Visual Basic EditorEach workbook has a single SheetChange event. To view or edit the subroutine that uses the SheetChange event:
This is the SheetChange subroutine for the active workbook. Whenever the SheetChange event occurs, this subroutine runs automatically.
NOTE: The first line of the subroutine (Private Sub... Excel.Range) should be entered on a single line without an underscore (_) character. What Are "Sh" and "Target"?The SheetChange subroutine accepts two arguments. By default, these arguments are Sh and Target.
Note that these arguments may be named differently; for example, you can
use SheetName and ChangedRange instead of Sh and Target. However, do not completely remove the arguments from the subroutine. If you do, you may receive the following error message:
Using the SheetChange SubroutineThe SheetChange subroutine works like any other Visual Basic for Applications subroutine in Microsoft Excel. You can use the Sh and Target arguments to determine the worksheet and range of cells that changed, or to determine the value of the changed cells. You can use these values to perform other actions.For example, the following subroutine opens a workbook when you type the name into a cell in a worksheet in the active workbook:
Because Target represents the changed cell, Target.Value returns the value
in that cell. If the workbook for the name you typed exists in the My
Documents folder on drive C, it is opened.
This example subroutine only accepts values that are less than or equal to 100:
If you type a value that is greater than 100 in a cell in the workbook, a
message box appears and the value is removed.
The following example subroutine runs another macro if you type a value in cell A1 of Sheet1:
If any other cell in the workbook is changed, the subroutine does nothing.
Additional query words: XL97
Keywords : kbprg kbui kbdta KbVBA |
Last Reviewed: September 22, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |