Restrict entering duplicate data in multiple column

amruthubballi

New Member
Joined
Apr 27, 2017
Messages
15
I want to restrict entering duplicate data in multiple column.

BCDF
20Client NameYearQuarterForm Type
21ABC2017-18Q126Q
22XYZ2017-18Q126Q
23ABC2017-18Q124Q
24ABC2017-18Q126Q

<tbody>
</tbody>

From the above table we can see that Row no’s 21, 22 & 23 are unique. In Row no’s 21 & 23 are almost same but they differ in form type hence they are also unique. Where as in Row no’s 21 & 24 they are exact same in all column so basically I want a VBA code which should restrict from entering same data twice in Table. If there is a change in one column is acceptable but it cannot be same in all column.

I have got a following code to restrict entering data in only one column

Private Sub Worksheet_Change(ByVal Target As Range)
If Application.CountIf(Range("B21:e120"), Target) > 1 Then
MsgBox "Dulipate Data!", vbCritical, "Remove Data"
Target.Value = ""
End If
End Sub

Please make necessary adjustment to restrict entering same data in Column B to F
I HOPE SOME ONE WILL HELP ME TO RESOLVE THIS
Thanks in Advance.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this:-
This code should delete any entered value in column "B:F" that turn that row into a duplicate row.
Code:
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_Change(ByVal Target [COLOR="Navy"]As[/COLOR] Range)
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
Application.EnableEvents = False
[COLOR="Navy"]If[/COLOR] Not Intersect(Target, Columns("B:F")) [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
   [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Txt = Join(Application.Transpose(Application.Transpose(Dn.Resize(, 5))), ",")
        [COLOR="Navy"]If[/COLOR] Not .Exists(Txt) [COLOR="Navy"]Then[/COLOR]
            .Add Txt, Nothing
        [COLOR="Navy"]Else[/COLOR]
            Target.ClearContents
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]End[/COLOR] If
Application.EnableEvents = True
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Please provide an explanation of how you propose the code should work in relation to your file sheet data
 
Upvote 0
Hi Mick,

Thank you again for reply… I hope you have seen the sheet I have sent…I am entering data by row wise. First I would enter client name, then year, quarter and then form type and rest of the details. In other words, I would enter by row no 21 complete all the details in that row then I would go for row no 22,23 so on as follows.


B
C
D
E
20
Client Name
Year
Quarter
Form Type
21
ABC
2017-18
Q1
26Q
22
XYZ
2017-18
Q1
26Q
23
ABC
2017-18
Q1
24Q
24
ABC
2017-18
Q1
26Q

<tbody>
</tbody>

From column B:E relates to a particular event. I am considering all the four columns as a one event which is supposed to be unique in table. I want a VBA code which should restrict from entering same data twice in column B:E

Eg 1: In row 21 & row 22 are completely different as they client name is different hence it is unique data.

Eg 2: In row 21 & row 23 are having same name, year and quarter but they have form type different hence still it is unique data.

But

Eg 3: In row 21 & row 24 are having same name, year and quarter and same form type as well so it is a duplicate data I would enter in the table.

So I want some code which should restrict from entering the same data twice in the table. In other words when I finish entering column E, VBA code should check the above data, if it is duplicate data I should display a message box as warning duplicate data and delete the data I just entered.

I hope this will help to understand what type of code I exactly want…please help!!

Thanks,
Amrut
 
Upvote 0
Try this code, it now start from row 21 and also provides a "MsgBox" when a duplicate is found.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, Dn As Range, Txt As String
Set Rng = Range(Range("B21"), Range("B" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
Application.EnableEvents = False
If Not Intersect(Target, Columns("B:E")) Is Nothing Then
   For Each Dn In Rng
    Txt = Join(Application.Transpose(Application.Transpose(Dn.Resize(, 5))), ",")
        If Not .Exists(Txt) Then
            .Add Txt, Nothing
        Else
            Target.ClearContents
        MsgBox "Duplicate Line !!"
        End If
    Next
End If
Application.EnableEvents = True
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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