Macro for data entry that checks for duplicate record

JRW520

New Member
Joined
Jan 15, 2010
Messages
13
Hello Forum!

I have a spreadsheet that uses a Form for Data entry. Each record has ID, Name, State, and about 20 more columns of information for each ID.

The data entry is working well.

What I need now is to add a check for a UNIQUE ID. If they are adding a new record I need my form to return a message if the ID they use is already in the database. So if they enter XXXXX and that is in the file already - they should get a message that XXXXX is already in the database and to please enter a new ID.

And once they fix XXXXX to XXXXY then the record is added.

I should add that the ID is stored in column A.

Thank you for your assistance!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Are you asking how to test if the new ID is unique? If yes, try one of these...

Code:
[color=green]'The find method[/color]
[color=darkblue]If[/color] [color=darkblue]Not[/color] Range("A:A").Find(What:="UniqueID", LookAt:=xlWhole, MatchCase:=False) [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color] MsgBox "Not Unique"

[color=green]'or CountIf function[/color]
If Application.Count[color=darkblue]If[/color](Range("A:A"), "UniqueID") > 0 [color=darkblue]Then[/color] MsgBox "Not Unique"

[color=green]'or Match Function[/color]
If IsNumeric(Application.Match("UniqueID", Range("A:A"), 0)) [color=darkblue]Then[/color] MsgBox "Not Unique"
 
Upvote 0
Copy to Worksheet code module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("A:A")) Is Nothing And Target <> "" Then
    NewVal = Target.Value
    Application.Undo
    If Application.CountIf(Range("A:A"), NewVal) <> 0 Then
        MsgBox "Entry Already Exists.  Verify Input", vbExclamation, "DUPLICATE ENTRY!"
        Application.EnableEvents = True
        Exit Sub
    Else
        Target = NewVal
    End If
End If
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Yes I am looking to test new ID as being unique. You stated that better than I!

I will give all your options a try and see which works the best. Thank you!!
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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