In Microsoft Excel, you can create a macro that is called only when a value is entered into a cell in a particular sheet or in any sheet that is currently open.
Note, however, that you should not call macros unnecessarily because they
slow down the performance of Excel.
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.
In many instances, a macro should run only when a certain number of cells
have values entered into them (referred to as the "key cells" in this
document). In order to prevent a large macro from running every time a
value is entered into a cell of a sheet, you must check to see if the
is one of the key cells. To accomplish this, use the
method on the ActiveCell
and the range containing the key cells to verify the ActiveCell
is one of the key cells. If the ActiveCell
is in the range containing the key cells, you can call the macro.
To run this sample macro:
- Open a new workbook.
- On Sheet1 in cell A11, type the following formula:
- Copy this formula to cells B11, C11, and D11.
In the example used here, the macro causes the background color
of cells A11, B11, and C11 to change to red (or ColorIndex 3) if that cell's value is larger than 50; otherwise, the cell's background color
will be normal. This is accomplished by checking for entries in
cells A1:A10, B1:B10, and C1:C10. Each time an entry is made in
any cell on Sheet1, the DidKeyCellsChange macro runs. This macro
determines if the ActiveCell is in the range of key cells (A1:C10). If
it is, the KeyCellsChanged macro is called to re-evaluate cells
The following macros work together to perform this functionality.
The "Auto_Open" macro must be run once in order for the other macros
to be called.
- Insert a new module sheet. In Microsoft Excel 97, on the Tools menu, point to Macro, and then click Visual Basic Editor. In the Editor, click Module on the Insert menu. In Microsoft Excel versions 5.0 and 7.0, point to Macro on the Insert menu, and then click Module.
- Type the following macro code into the module sheet:
' Run the macro DidCellsChange any time a entry is made in a
' cell in Sheet1.
ThisWorkbook.Worksheets("Sheet1").OnEntry = "DidCellsChange"
Dim KeyCells as String
' Define which cells should trigger the KeyCellsChanged macro.
KeyCells = "A1:A10, B1:B10, C1:C10"
' If the Activecell is one of the key cells, call the
' KeyCellsChanged macro.
If Not Application.Intersect(ActiveCell, Range(KeyCells)) _
Is Nothing Then KeyCellsChanged
Dim Cell as Object
' If the values in A11:C11 are greater than 50...
For Each Cell In Range("A11:C11")
If Cell > 50 Then
' Make the background color of the cell the 3rd color on the
' current palette.
Cell.Interior.ColorIndex = 3
' Otherwise, set the background to none (default).
Cell.Interior.ColorIndex = xlNone
- Save the workbook, and then close it.
- Open the workbook.
This will cause the Auto_Open macro to run.
- In cell A1, type 60.
Cell A11 should change to a Red background color because you changed
a cell in the KeyCells range and the value of cell A11 became greater
- In cell D1, type 60
Nothing will happen to the color of D11, even though the value in D11
is now greater than 50. Nothing happens because cell D1 is not in the
In Microsoft Excel version 7.0, for more information about running
procedures when an event occurs, click the Index
tab in Microsoft Excel Help, type the following text
double-click the appropriate text to go to the "Running procedures when
an event occurs"
Article ID: 142154 - Last Review: August 17, 2005 - Revision: 2.1
- Microsoft Excel 98 for Macintosh
- Microsoft Excel 97 Standard Edition
- Microsoft Excel 95 Standard Edition
- Microsoft Excel 5.0 Standard Edition
- Microsoft Excel 5.0c
- Microsoft Excel 5.0 for Macintosh
- Microsoft Excel 5.0a for Macintosh
- Microsoft Excel 5.0 Standard Edition
|kbdtacode kbhowto kbprogramming KB142154|Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.