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?
Your code looks pretty good.
Be careful, however, for your call to
Range("H5")is a shortcut command toApplication.Range("H5"), which is equivalent toApplication.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"):Or you can use
Me.Range("H5"), if the event handler is on the code page for the worksheet in question (it usually is):