VBA data validation

Gabriel222

New Member
Joined
Oct 24, 2008
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello,


I'm a little new to VBA, and i ran into a piece of VBA code which would prevent my data validation from being erased (either through a copy/paste or other).

I implemented the code (i believe in the right place, although i'm not sure), and upon testing the code's VBA generated message box came up about a hundred times, and basically forced me to restart excel:confused:.

Does anybody have any suggestions as to make this piece of code work?

Private Sub Worksheet_Change(ByVal Target As Range)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Dim VT As Long<o:p></o:p>
'Do all cells in the validation range<o:p></o:p>
'still have validation?<o:p></o:p>
On Error Resume Next<o:p></o:p>
VT = Range("InputRange").Validation.Type<o:p></o:p>
If Err.Number <> 0 Then<o:p></o:p>
Application.Undo<o:p></o:p>
MsgBox "Your last operation was canceled." & _<o:p></o:p>
"It would have deleted data validation rules.", vbCritical<o:p></o:p>
End If<o:p></o:p>
End Sub<o:p></o:p>

Thank you
 
Last edited:

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.
Try using the line:

Application.EnableEvents = False

before the "Dim" line and:

Application.EnableEvents = True

before the "End Sub" line.

What is probably happening is that when the code changes values of a cell (i.e. during the undo command) it is triggering the code to run again. This method will stop the code recursively calling itself.
 
Upvote 0
thanks for the idea

although it did prevent the recursive message box,
it now forbids me to delete a cell

whereas i'm trying to prevent any change of my data validation and only my data validation... :oops:
 
Upvote 0
I'm also trying to make this work, and at least for me, the error message comes up all the time, regardless whether it's a valid entry or not.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

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