Run macro only if value in cell increases

ENTERTAIN

New Member
Joined
Sep 6, 2017
Messages
29
This code activates my nominated sub on the sheet when I make any change to cells B9:AJ9,B10:AJ10, but what what I need is for the nominated sub to run only when the values in those cells increase and not when the values decrease. Could anyone please show me what I change to make this happen?

Private Sub Worksheet_Change (ByVal Target As Range)
If Not Intersect (Target, Range("B9:AJ9,B10:AJ10)) Is Nothing Then
Call MySub
End If
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
One way is to use the Undo to get the old value:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    If Not Intersect(Target, Range("B9:AJ9,B10:AJ10")) Is Nothing And Target.Count = 1 Then
        Application.Undo
        oldVal = Target.Value
        Application.Undo
        If oldVal < Target.Value Then Call MySub
    End If
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
This is assuming you are only changing the cells one at a time.
 
Upvote 0
One way is to use the Undo to get the old value:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    If Not Intersect(Target, Range("B9:AJ9,B10:AJ10")) Is Nothing And Target.Count = 1 Then
        Application.Undo
        oldVal = Target.Value
        Application.Undo
        If oldVal < Target.Value Then Call MySub
    End If
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
This is assuming you are only changing the cells one at a time.
I am only changing values one cell at a time. I will give that a try. Thanks very much for your input.
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top