PDA

View Full Version : Prevent duplicate but for one cell only



suny100
06-07-2011, 01:49 PM
here is code for prevent duplicates in sheet upon coulmn 1 but it works just if iam pasting one cell only but if i paste more than 1 it not work , could anybody help me?

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Finish
If Target.Column = 1 Then
For Each cell In Selection
If Application.WorksheetFunction.CountIf(Columns(1), cell.Value) > 1 Then
Application.Undo
MsgBox "You were trying to paste in duplicate entries"
End If
Next cell
End If
Finish:
Application.EnableEvents = True
End Sub

Chabu
06-07-2011, 02:37 PM
replace "selection" with "Target" like this and it works fine
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
On Error GoTo Finish
If Target.Column = 1 Then
For Each aCell In Target
If Application.WorksheetFunction.CountIf(Columns(1), aCell.Value) > 1 Then
Application.Undo
MsgBox "You were trying to paste in duplicate entries"
End If
Next aCell
End If
Finish:
Application.EnableEvents = True
End Sub


Greetings

suny100
06-08-2011, 02:54 PM
replace "selection" with "Target" like this and it works fine
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
On Error GoTo Finish
If Target.Column = 1 Then
For Each aCell In Target
If Application.WorksheetFunction.CountIf(Columns(1), aCell.Value) > 1 Then
Application.Undo
MsgBox "You were trying to paste in duplicate entries"
End If
Next aCell
End If
Finish:
Application.EnableEvents = True
End Sub


Greetings


Thanks for your help ,but it not work fine as it prevent all data even not duplicate (ie. if iam trying to paste 6 cells contain 1 duplicate and 5 unique, it prevent all of them not just the 1 that duplicated)

mikerickson
06-08-2011, 03:27 PM
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
On Error Goto Finish
Application.EnableEvents = False
If Target.Column = 1 Then
For Each aCell In Target
If Application.WorksheetFunction.CountIf(Columns(1), aCell.Value) > 1 Then
aCell.ClearContents
End If
Next aCell
End If
Finish:
Application.EnableEvents = True
End Sub

suny100
06-09-2011, 07:49 AM
mikerickson, thanks for your reply but it also not work it clear all of them also not duplicated values only

CharlesH
06-09-2011, 10:19 AM
Perhaps a copy of your workbook so we can see what your working with?