I don't know of a way to get around the Undo. In theory, you can use the object model to read the data validation and replicate it in code, I don't know that I'm ready to do that here as it would take a bit of delving. I've created validation routines for userforms before that associate text boxes with a validation type, then just validate against that type (useful when there are 200+ text boxes). Something similar could be done here, identify the cells that use a particular type of validation (numeric only, date ranges, allowed ranges). The ValidValue routine would then be extended to check which type of validation is used for each target cell and perform the desired validation. Something like:
Then define a your validation set. I put mine on a different sheet, looking something like this:
Min Max Ranges
Numeric 1 1 31 B2:B4
Numeric 2 0 100 B5:B8
Numeric 3 0 1000 B9:B12
Date 1 4/9/2012 5/9/2012 B13:B14
With column A being "Numeric" or "Date", an identifer (1-3) in column B, the minimum value in column C maximum value in column D and the range to apply the validation type to in column E. For my dates, I just used =Today()-30 and =Today() to keep the range dynamic. The Ranges column can be of the common methods for spelling out ranges, including commas and colons:
B5,B6,B10,C18:C20
would work just fine. I then created the named range "ValidTypes" for A2:E5.
Actually using this, you could just remove the data validation and tweak the worksheet_change event to call this instead. There are some optimizations that could be done, if you run into performance issues.
Bookmarks