Unique Name Entry, Take Two

Written by Allen Wyatt (last updated December 16, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


Chris uses a data validation technique that successfully stops non-unique information from being entered in a column. (This technique was described in previous issues of ExcelTips.) He rightfully notes that there is still a problem with data validation, however: Someone can paste information into a cell and successfully bypass all the checks in place.

For instance, if you type "George" into cell A8, and then type "George" into A9, regular data validation will generate an error, as one would expect, indicating that the value you are trying to enter is not unique. However, if you type "George" into cell A8, copy that cell and paste it into cell A9, no data validation error is triggered--the paste is allowed.

There is no direct way around this in Excel. You can, however, cause Excel to do some checking whenever you try to do a paste. Consider the following macro:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    For Each TmpRng In Target
        TmpVal = TmpRng.Validation.Type
        If TmpVal > 0 Then
            If Application.CutCopyMode = 1 Then
                MsgBox "You cannot paste into validated cells."
                Application.CutCopyMode = False
                Exit Sub
            End If
        End If
    Next
End Sub

This macro is only run when the selection changes in a worksheet. (This code needs to be in the code window for a worksheet.) It examines the target cells (the ones being selected), and if the user is trying to paste into a cell that has validation active, it will not allow it. Further, the user will see a dialog box that indicates the error.

You should note that this routine just checks to see if pasting into a data-validated cell is being done. If it is, then an error is generated. The routine does not check to see if what is being pasted is actually permissible under the validation rules in the target cells; that would be much more complex and require quite a bit more coding.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2449) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Turning Off Default First Page Numbering

Ever want to change the default settings for how Word handles page numbering? Word doesn't make this as easy as you would ...

Discover More

Making the 'Welcome Back' Message Consistent and Permanent

When you open a document on which you previously worked, Word displays a "Welcome back" message that can help return you ...

Discover More

Weird Hyperlink Behavior

When you insert a hyperlink, you expect it to look like, well, a hyperlink. But what if it really looks like some strange ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Entering Info into Multiple Cells

Want to make an entry of the same value into a group of selected cells? It's easy to do with just one small change in how ...

Discover More

Copying to Very Large Ranges

Using the mouse to select a large cell range can be frustratingly slow. If you want to make copying to a large range of ...

Discover More

Getting Rid of Non-Printing Characters Intelligently

Is your worksheet, imported from an external source, plagued by non-printing characters that show up like small boxes ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.