Application.Undo Error

jsahmed

New Member
Joined
Aug 4, 2016
Messages
3
I am attempting to write a function that does not allow users to copy text into a forced data validation. For some reason I am receiving an Application.Undo error? Can anyone provide guidance? Thanks.

Private Sub Worksheet_Change(ByVal Target As Range)
'Does the validation range still have validation?
If HasValidation(Range("ValidationRange")) Then
Exit Sub
Else
Application.Undo
MsgBox "Your last operation was canceled." & _
" It would have deleted data validation rules. Please select from the dropdown menu.", vbCritical
End If
End Sub


Private Function HasValidation(r) As Boolean
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
x = r.Validation.Type
If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You need to disable events before the undo so that the undo doesn't trigger the change event code again. Try:

Else
with Application
.EnableEvents = False
.Undo
.EnableEvents = True
End with
 
Upvote 0
Thanks for your help JoMoe. For some reason I am still receiving a debugging error with the .Undo

My apologies I have never used this function before


Private Sub Worksheet_Change(ByVal Target As Range)
'Does the validation range still have validation?
If HasValidation(Range("ValidationRange")) Then
Exit Sub
Else
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
MsgBox "Your last operation was canceled." & _
" It would have deleted data validation rules. Please select from the dropdown menu.", vbCritical
End If
End Sub
 
Upvote 0
I cannot reproduce that error so can't tell you why you are getting it. One point to note is that your function HasValidation will return True only if EVERY CELL in the range r has IDENTICAL validation. Might be better to apply it to just the target range like this:

If HasValidation(Range(Target)) Then
Exit Sub
......
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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