Worksheet Change

DrH100

Board Regular
Joined
Dec 30, 2011
Messages
78
Morning all

I was very kindly provided the code below which produces a message should the value of one cell (row 39) exceed the value of another (row 41) as users change data in a column. That works great.

What I would like to do is change it ever so slightly in that I would like the message to only appear if the user is "adding" data to the column rather than if they are removing. Ie if they remove data from the column but row 39 still exceeds row 41 no message is shown (as they are making the position better).

Is this possible at all?

The code I am using is below.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRange As Range
   Dim col As Long

'  Set range to apply this to
   Set myRange = Range("E47:BB1000")

'  Exit if more than one cell updated at a time
If Target.CountLarge > 1 Then Exit Sub

' Exit if cell updated is outside of designated range
If Intersect(Target, myRange) Is Nothing Then Exit Sub

'Get number of updated column
col = Target.Column

'Check to see if row 1 is greater than row 2 in that column
If Cells(39, col) > Cells(41, col) Then
MsgBox "This entry exceeds the agreed threshold, If you are adding leave please ensure this is has been agreed with your line manager", vbOK


'If answer = vbNo Then

Else

   End If

End Sub

As always, any help appreciated.

Thanks
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Does this achieve what you want ?

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim myRange As Range
    Dim col As Long

'  Set range to apply this to
    Set myRange = Range("E47:BB1000")
'  Exit if more than one cell updated at a time
    If Target.CountLarge > 1 Then Exit Sub
' Exit if cell updated is outside of designated range
    If Intersect(Target, myRange) Is Nothing Then Exit Sub

' Exit if cell contains no value
    If Target.Value = "" Then Exit Sub

' Get number of updated column
    col = Target.Column
' Check to see if row 1 is greater than row 2 in that column
    If Cells(39, col) > Cells(41, col) Then
        MsgBox "This entry exceeds the agreed threshold, If you are adding leave please ensure this is has been agreed with your line manager", vbOK
' If answer = vbNo Then
    Else

    End If

End Sub
 
Upvote 0
I was thinking that entries in the column are causing row 39 to increase or decrease.
So maybe this?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRange As Range
Dim col As Long
Dim PrevVal As Variant
'  Set range to apply this to
   Set myRange = Range("E47:BB1000")

'  Exit if more than one cell updated at a time
If Target.CountLarge > 1 Then Exit Sub

' Exit if cell updated is outside of designated range
If Intersect(Target, myRange) Is Nothing Then Exit Sub

'Get number of updated column
col = Target.Column
'Get previous value of row 39
Application.EnableEvents = False
  Application.Undo
  PrevVal = Cells(39, col)
  Application.Undo
Application.EnableEvents = True

'Check to see if row 1 is greater than row 2 in that column  AND of increases value
If Cells(39, col) > Cells(41, col) And Cells(39, col) > PrevVal Then

MsgBox "This entry exceeds the agreed threshold, If you are adding leave please ensure this is has been agreed with your line manager", vbOK


'If answer = vbNo Then

Else

   End If

End Sub
 
Upvote 0
Does this achieve what you want ?

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim myRange As Range
    Dim col As Long

'  Set range to apply this to
    Set myRange = Range("E47:BB1000")
'  Exit if more than one cell updated at a time
    If Target.CountLarge > 1 Then Exit Sub
' Exit if cell updated is outside of designated range
    If Intersect(Target, myRange) Is Nothing Then Exit Sub

' Exit if cell contains no value
    If Target.Value = "" Then Exit Sub

' Get number of updated column
    col = Target.Column
' Check to see if row 1 is greater than row 2 in that column
    If Cells(39, col) > Cells(41, col) Then
        MsgBox "This entry exceeds the agreed threshold, If you are adding leave please ensure this is has been agreed with your line manager", vbOK
' If answer = vbNo Then
    Else

    End If

End Sub
Sorry its taken a couple of days to reply but that works perfectly - tvm
 
Upvote 0
I was thinking that entries in the column are causing row 39 to increase or decrease.
So maybe this?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRange As Range
Dim col As Long
Dim PrevVal As Variant
'  Set range to apply this to
   Set myRange = Range("E47:BB1000")

'  Exit if more than one cell updated at a time
If Target.CountLarge > 1 Then Exit Sub

' Exit if cell updated is outside of designated range
If Intersect(Target, myRange) Is Nothing Then Exit Sub

'Get number of updated column
col = Target.Column
'Get previous value of row 39
Application.EnableEvents = False
  Application.Undo
  PrevVal = Cells(39, col)
  Application.Undo
Application.EnableEvents = True

'Check to see if row 1 is greater than row 2 in that column  AND of increases value
If Cells(39, col) > Cells(41, col) And Cells(39, col) > PrevVal Then

MsgBox "This entry exceeds the agreed threshold, If you are adding leave please ensure this is has been agreed with your line manager", vbOK


'If answer = vbNo Then

Else

   End If

End Sub
Thanks - I haven't tried this one as the earlier response sorted my issue but out of interest I will try this one at a later date and see - TVM
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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