XL: How to Run a Macro When Certain Cells ChangeID: Q142154
|
In Microsoft Excel, you can create a macro that will be 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
will slow down the performance of Microsoft Excel.
Microsoft 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.aspIn 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 ActiveCell is one of the key cells. This can be accomplished by using the Intersect 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.
Sub auto_open()
' Run the macro DidCellsChange any time a entry is made in a
' cell in Sheet1.
ThisWorkbook.Worksheets("Sheet1").OnEntry = "DidCellsChange"
End Sub
Sub 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
End Sub
Sub 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
Else
' Otherwise, set the background to none (default).
Cell.Interior.ColorIndex = xlNone
End If
Next Cell
End Sub
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
procedures, running
double-click the appropriate text to go to the "Running procedures when
an event occurs" topic.
Additional query words: 5.00a 5.00c 8.00 xl97
Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA
Version : MACINTOSH:5.0,5.0a,98; WINDOWS:5.0,5.0c,7.0,97; winnt:5.0
Platform : MACINTOSH WINDOWS winnt
Issue type : kbhowto
Last Reviewed: August 3, 1999