No Duplications

srizki

Well-known Member
Joined
Jan 14, 2003
Messages
1,844
Office Version
  1. 365
Platform
  1. Windows
I have to type a an ID in column D of a worksheet called “restricted funds”, I have 13 sheets in that workbook and each sheet has in column D some IDs, I can not type a duplicate ID, in the that sheet. What is the best way to avoid no duplications. IDs are simply numbers.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
Thanks
 
you're welcome!!!

instead of putting the code in all sheets, you can put it in the workbook module - with little changes

is it all sheets without exceptions?
else list the exceptions please
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Thanks Erik,

There are no exceptions. I want to use the code in ThisWorkbook module.
 
Upvote 0
delete all similar code in all sheet modules
put this in the workbook module
Code:
Option Explicit
 
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Erik Van Geit
'080624 2313
'no duplicates allowed on this sheet or other sheets
'within the range "RngAddress"
'multiple changes within range not allowed, unless clearing data
 
Dim sht As Worksheet
Dim dup As Range
Dim RngAddress As String
Dim msg As String
 
RngAddress = "D1:D" & Rows.Count     'range to check
 
    If Intersect(Target, Range(RngAddress)) Is Nothing Then Exit Sub
 
    If Application.CountA(Target) = 0 Then Exit Sub
    If Target.Count > 1 Then
    msg = "Please change only one item at a time in the range " & RngAddress
 
    Else
 
        For Each sht In ThisWorkbook.Worksheets
            If sht.Name <> Sh.Name Then
            Set dup = sht.Range(RngAddress).Find(What:=Target, LookIn:=xlFormulas, LookAt:=xlWhole)
                If Not dup Is Nothing Then
                msg = "The item """ & Target & """ can be found on sheet """ & sht.Name & """ in cell " & dup.Address(0, 0)
                Exit For
                End If
            Else
                If Application.CountIf(sht.Range(RngAddress), Target) > 1 Then
                msg = "The item """ & Target & """ is already in the list on this sheet."
                Exit For
                End If
            End If
        Next sht
 
    End If
 
    If Len(msg) > 0 Then
        With Application
        .EnableEvents = False
        .Undo
        .EnableEvents = True
        End With
    MsgBox msg, vbCritical, "ERROR"
    End If
 
End Sub
 
Upvote 0
It worked perfectly. If I were to use it on other wb. the only change I will make is "D1:D", is that right? Erik.

Thanks
 
Upvote 0
Oh Yes, Erik, I changed D to M and it worked, If I do not want duplicate in a range other than a column, can I put range such as D4:K32?
I have not tried yet, but that would be a plus too.

Thanks
 
Upvote 0
that was great help Erik, I learned something new the next thing is I have to understand the coding.
Thank you very much.

Regards
Sohail.
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,321
Members
449,154
Latest member
pollardxlsm

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