Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

How can I automatically execute an Excel macro each time a value in a particular cell changes?

Right now, my working code is:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("H5")) Is Nothing Then Macro
End Sub

where "H5" is the particular cell being monitored and Macro is the name of the macro.

Is there a better way?

share|improve this question
    
Does the RunMacroWhenValueChanges UDF in FormulaDesk satisfy your requirements? formuladesk.com – Gareth Hayter Jun 3 '15 at 4:08
    
Possible duplicate of excel VBA run macro automatically whenever a cell is changed – cemper93 Feb 2 '16 at 13:44
up vote 83 down vote accepted

Your code looks pretty good.

Be careful, however, for your call to Range("H5") is a shortcut command to Application.Range("H5"), which is equivalent to Application.ActiveSheet.Range("H5"). This could be fine, if the only changes are user-changes -- which is the most typical -- but it is possible for the worksheet's cell values to change when it is not the active sheet via programmatic changes, e.g. VBA.

With this in mind, I would utilize Target.Worksheet.Range("H5"):

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("H5")) Is Nothing Then Macro
End Sub

Or you can use Me.Range("H5"), if the event handler is on the code page for the worksheet in question (it usually is):

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("H5")) Is Nothing Then Macro
End Sub

Hope this helps...

share|improve this answer
3  
what if cell H5 is changed from another sheet, say sheet2 then the above function doesn't work. plz help in this. – dhpratik Sep 30 '13 at 9:58
    
+1 for the insight that Range(x) is equivalent to Application.Range(x) which is equivalent to Application.ActiveSheet.Range(x). – Will Ediger May 7 '15 at 17:52
1  
For anyone coming here from a google search, make sure you paste this code into the sheet in vba, not a module like I did. look at stackoverflow.com/questions/15337008/… – hammythepig Apr 27 '16 at 21:14
    
Application.ActiveSheet.Range("H5"). ==> target.parent.range("H5") is even safer – Pierre Sep 30 '16 at 15:44

Handle the Worksheet_Change event or the Workbook_SheetChange event.

The event handlers take an argument "Target As Range", so you can check if the range that's changing includes the cell you're interested in.

share|improve this answer
    
Thanks, it works. I check the range with, say, Target.Address = Range("H5").Address. Is there an easier way? – namin Jan 3 '09 at 21:33
    
An alternative: Not (Intersect(Target, Range("H5")) Is Nothing) . Is this how you would do it? – namin Jan 3 '09 at 21:38
2  
The first comment (Target.Address = Range("H5").Address) wouldn't work if your cell was only part of the changed range. The second comment still suffers the problems described by Mike Rosenblum. – Ant Apr 27 '09 at 10:45

I prefer this way, not using a cell but a range

    Dim cell_to_test As Range, cells_changed As Range

    Set cells_changed = Target(1, 1)
    Set cell_to_test = Range( RANGE_OF_CELLS_TO_DETECT )

    If Not Intersect(cells_changed, cell_to_test) Is Nothing Then 
       Macro
    End If
share|improve this answer
    
It's the same as one cell. You can either set a range as one cell, range of continuos cells, or even scattered cells (all separated by comma). – Shai Alon Dec 20 '15 at 9:58

I have a cell which is linked to online stock database and updated frequently. I want to trigger a macro whenever the cell value is updated.

I believe this is similar to cell value change by a program or any external data update but above examples somehow do not work for me. I think the problem is because excel internal events are not triggered, but thats my guess.

I did the following,

Private Sub Worksheet_Change(ByVal Target As Range) 
  If Not Intersect(Target, Target.Worksheets("Symbols").Range("$C$3")) Is Nothing Then
   'Run Macro
End Sub
share|improve this answer
    
I can not get this to work for some reason. When I tell the code to run in VBA it is pulling up a pop up menue and asking me if I want to run the macro instead of running the macro automaticaly? – David Van der Vieren Mar 28 '13 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.