Worksheet Change Event Triggering With Cell Validation "Cancel"

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a column (A) of cells for which I have a data validation rule applied. As it is, if the user enters an invalid value into a cell in that column, the user is able to select "Retry, Cancel" or "Help".
I also have code set up so that if the value in any of the cells in column A change, code is triggered. I'm finding, that if the user enters an erroneous value, trapped by the validation rule, and the user selects cancel, that it triggers the worksheet change event for the change of that target.

How can I prevent cancel from triggering the code?

Code:
Sub Worksheet_Change(ByVal Target As Range)
    Dim cval As String, aval As String
    Dim bval As String
    Dim msg1 As String, msg2 As String, msg3 As String
    Dim acnt As Long
    If Not Application.Intersect(Columns(1), Range(Target.Address)) Is Nothing Then
Stop
        aval = "R" & Target.Value
        acnt = Application.WorksheetFunction.Match(aval, ws_pdata.Columns(1), 0)
        If acnt > 1 Then
            MsgBox "permit already exists in database."
            Exit Sub
        End If
    ElseIf Not Application.Intersect(Columns(3), Range(Target.Address)) Is Nothing Then
Stop
        cval = Target.Value
        
    ElseIf Not Application.Intersect(Columns(2), Range(Target.Address)) Is Nothing Then
    . . . .
    End If
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
How can I prevent cancel from triggering the code?

So long as events are enabled, you can't. The change event is triggered on any change to the cell, even a wrong one that the validation rules reject. What you have to do is add some code to Sub Worksheet_Change to intercept that condition. For example, selecting cancel will clear the bad entry from the cell, so something like this might work for you.
VBA Code:
    If Not Application.Intersect(Columns(1), Range(Target.Address)) Is Nothing Then
        If IsEmpty(Target) Then
            Exit Sub
        End If
 
Upvote 0

Forum statistics

Threads
1,223,867
Messages
6,175,063
Members
452,611
Latest member
bls2024

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