Problems trapping Cancels

BillySilly

New Member
Joined
Jan 23, 2013
Messages
49
Hi
I have a cell L5 validated for a Date and the following code comes into play if the cell contents is changed.

On the sheet code page

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$L$5" Then Call Change
End Sub

On a regular module

Code:

Sub Change()
MsgBox "Hello" ‘code will be something useful
End Sub


If I select the cell and start to change it by clicking in the formula bar I get the OK tick and Cancel X on the formula bar.
If without changing it I click the OK tick or if I click on the Cancel X it leaves the cell unchanged and Change is not called….fine
If I enter a valid date and click the OK tick Change is called…. fine
If I enter an invalid date and click the OK tick it returns the validation Msg Box with buttons for Retry and Cancel but puts the invalid date in L5. When I do enter a valid date Change is called…..fine
If I click Cancel in the Msg box or Cancel (X) in the formula bar it reverts to the original L5 contents which triggers Change….infine
How do I modify the code so that neither of the Cancels will trigger Change?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
It would have been handy if you included the actual code for Sub Change()

Bit it sounds like the code is altering the cell contents...? Clearing it, for example?

If so, use
Code:
Application.EnableEvents = False

You must re-enable EventHandling before exiting the procedure. Ideally, you should include an error handler as the Application state is being changed. The Error trap should ensure any Application properties are reset before the procedure exits.
 
Last edited:
Upvote 0
Hi
The macro Change has nothing in it at the moment as it just shows me that the changed cell event has been recognised.
I am still a relative newbie
I have also just noticed that if an invalid date has been entered I get the MsgBox and if I click on Retry, Change is called twice. If I retry twice it is called three times.
 
Upvote 0
Sorry - I can't replicate that behavior in XL 2007, even with Data Validation for dates set in the cell.
 
Upvote 0

Forum statistics

Threads
1,215,428
Messages
6,124,832
Members
449,190
Latest member
rscraig11

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