+ Reply to Thread
Results 1 to 6 of 6

code to prevent duplicate entries

  1. #1
    Registered User
    Join Date
    11-19-2006
    Posts
    26

    code to prevent duplicate entries

    I have the below code that prevents a user from entering duplicate data within a column for each worksheet of the workbook. It is placed within the ThisWorkbook module. Columns A and B are excluded. I am wondering if there is a way to exclude certain worksheets. So if I have 5 sheets, is there a way to make the below code apply to only sheet numbers 1 and 3, for example.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If IsEmpty(Target.Value) Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    If Target.Column < 3 Then Exit Sub
    With Range(Cells(1, Target.Column).Address & ":" & Cells(Target.Row - 1, Target.Column).Address & "," & Cells(Target.Row + 1, Target.Column).Address & ":" & Cells(Rows.Count, Target.Column).Address)
    Set c = .Find(Target.Value, , , xlWhole)
    If Not c Is Nothing Then
    MsgBox "Data already exists at range: " & c.Address(0, 0)
    Target.Value = ""
    End If
    End With
    End Sub

  2. #2
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Just add a test on the Sh object ...

    HTH
    Carim

  3. #3
    Registered User
    Join Date
    11-19-2006
    Posts
    26

    re: code to prevent duplicate entries

    Any hints on how to do that?

  4. #4
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    If not (sh.Name = "Sheet1" or sh.Name = "Sheet3") then
    ...
    End if

    HTH
    Carim

  5. #5
    Registered User
    Join Date
    11-19-2006
    Posts
    26

    re: code to prevent duplicate entries

    thanks. that works great

  6. #6
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Glad your problem is fixed

    Thanks for the feedback

    Carim

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1