Macro to prevent duplicate entries

y3kesprit

Board Regular
Joined
Mar 23, 2010
Messages
133
Hello all,

I am currently using this macro to prevent users from entering similar entries within a spreadsheet.

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
' Vasant Nanavati 2002
On Error GoTo ErrorHandler
If Not Intersect(Target, Columns(1)) Is Nothing Then
If Not Range(Cells(1, 1), Cells(Intersect _
(Target, Columns(1)).Row - 1, 1)).Find _
(Target.Value, LookIn:=xlValues, LookAt:= _
xlWhole) Is Nothing Then
MsgBox "Part no. already exists!"
Application.EnableEvents = False
With Intersect(Target, Columns(1))
.ClearContents
.Select
End With
End If
End If
ErrorHandler:
Application.EnableEvents = True
End Sub

However, the problem I am facing is that its effect is for the whole spreadsheet. How can I limit this macro to one column only (say column B)?

Thanks for the help!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
This does not affect the entire sheet as you say, it only deals with column A.

Change this
If Not Intersect(Target, Columns(1)) Is Nothing Then

to this
If Not Intersect(Target, Columns(2)) Is Nothing Then

though data validation would be a lot less hassle.
 
Upvote 0
This does not affect the entire sheet as you say, it only deals with column A.

Change this
If Not Intersect(Target, Columns(1)) Is Nothing Then

to this
If Not Intersect(Target, Columns(2)) Is Nothing Then

though data validation would be a lot less hassle.

Thanks for the prompt reply.

I would use data validation if I could. But right now I am dealing with drop down menus, so my data validation is, in that sense, used up.

Thank you anyway!
 
Upvote 0
This code is more efficient if you have your heart set on VBA.

Code:
 Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Column <> 2 Or .Cells.Count > 1 Then Exit Sub
If WorksheetFunction.CountIf(Columns(.Column), .Value) > 1 Then
Application.DisplayAlerts = False
.ClearContents
Application.DisplayAlerts = True
MsgBox "Part no. already exists!"
End If
End With
End Sub
 
Upvote 0
This does not affect the entire sheet as you say, it only deals with column A.

Change this
If Not Intersect(Target, Columns(1)) Is Nothing Then

to this
If Not Intersect(Target, Columns(2)) Is Nothing Then

though data validation would be a lot less hassle.


Not sure why, but that edit doesn't seem to solve the problem :confused:
 
Upvote 0
This code is more efficient if you have your heart set on VBA.

Code:
 Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Column <> 2 Or .Cells.Count > 1 Then Exit Sub
If WorksheetFunction.CountIf(Columns(.Column), .Value) > 1 Then
Application.DisplayAlerts = False
.ClearContents
Application.DisplayAlerts = True
MsgBox "Part no. already exists!"
End If
End With
End Sub

I was about to tear the little hair I have left completely out when I came across this code.
I have 'hijacked' it....hope that is ok with you?
Amazing...simple and efficient.
Thank you!!
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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