+ Reply to Thread
Results 1 to 7 of 7

Duplicate Value warning

  1. #1
    yukon_phil
    Guest

    Duplicate Value warning

    I have used the following code to give me a warning of duplicated values in a
    column, at least I thought it should do this. It does in fact give me the
    warning in the column BUT it give me the warning if in the same row and this
    is permitted.

    How do I get the code correct so it just gives me the warning for the column
    values and not the row? Thanks

    Private Sub Worksheet_Change(ByVal Target As Range)
    'Adjust next constant to your own needs
    Const myColumn As String = "B:AF"
    Dim rng As Range
    Dim Found As Range

    Set rng = UsedRange.Columns(myColumn)
    If Intersect(Target, rng) Is Nothing _
    Or Target.Value = "" _
    Then Exit Sub
    Set Found = rng.Find(Target.Value)
    If Found.Address <> Target.Address Then
    Target.Select
    MsgBox ("Duplicate code")
    End If
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,065
    If youre just checking for a column of duplicates shouldnt that "B:AF" be "B:B"?
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    yukon_phil
    Guest

    Re: Duplicate Value warning

    If I use B:B would that not just give me the warn for duplicate values in
    column "B" only, I also need to know if there are duplicate values in all
    columns from B to AF but not in any row. Does this make sense?

    "Special-K" wrote:

    >
    > If youre just checking for a column of duplicates shouldnt that "B:AF"
    > be "B:B"?
    >
    >
    > --
    > Special-K
    >
    >
    > ------------------------------------------------------------------------
    > Special-K's Profile: http://www.excelforum.com/member.php...fo&userid=7470
    > View this thread: http://www.excelforum.com/showthread...hreadid=562514
    >
    >


  4. #4
    vezerid
    Guest

    Re: Duplicate Value warning

    I think it should work if you change the line:

    Set rng = UsedRange.Columns(myColumn)

    with

    Set rng = Target.cells(1,1).EntireColumn

    Does this help?
    Kostis Vezerides


  5. #5
    Tom Hutchins
    Guest

    RE: Duplicate Value warning

    How about this instead?

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Application.WorksheetFunction.CountIf(Columns(Target.Column),
    Target.Value) > 1 Then
    MsgBox "Duplicate entry"
    End If
    End Sub

    Hope this helps,

    Hutch

    "yukon_phil" wrote:

    > I have used the following code to give me a warning of duplicated values in a
    > column, at least I thought it should do this. It does in fact give me the
    > warning in the column BUT it give me the warning if in the same row and this
    > is permitted.
    >
    > How do I get the code correct so it just gives me the warning for the column
    > values and not the row? Thanks
    >
    > Private Sub Worksheet_Change(ByVal Target As Range)
    > 'Adjust next constant to your own needs
    > Const myColumn As String = "B:AF"
    > Dim rng As Range
    > Dim Found As Range
    >
    > Set rng = UsedRange.Columns(myColumn)
    > If Intersect(Target, rng) Is Nothing _
    > Or Target.Value = "" _
    > Then Exit Sub
    > Set Found = rng.Find(Target.Value)
    > If Found.Address <> Target.Address Then
    > Target.Select
    > MsgBox ("Duplicate code")
    > End If
    > End Sub


  6. #6
    yukon_phil
    Guest

    Re: Duplicate Value warning

    That did, thank you very much.

    As I go through the workings of these worksheets I am coming up with more
    and more issues I am unable to resolve. These discussions groups are such a
    valuable resource, I think you will be seeing more questions from me, hope I
    don't overdue it in people eyes, I am still learning.

    Thanks again

    "vezerid" wrote:

    > I think it should work if you change the line:
    >
    > Set rng = UsedRange.Columns(myColumn)
    >
    > with
    >
    > Set rng = Target.cells(1,1).EntireColumn
    >
    > Does this help?
    > Kostis Vezerides
    >
    >


  7. #7
    yukon_phil
    Guest

    RE: Duplicate Value warning

    Thanks for your input, but the code I received from Kostis worked perfectly.

    "Tom Hutchins" wrote:

    > How about this instead?
    >
    > Private Sub Worksheet_Change(ByVal Target As Range)
    > If Application.WorksheetFunction.CountIf(Columns(Target.Column),
    > Target.Value) > 1 Then
    > MsgBox "Duplicate entry"
    > End If
    > End Sub
    >
    > Hope this helps,
    >
    > Hutch
    >
    > "yukon_phil" wrote:
    >
    > > I have used the following code to give me a warning of duplicated values in a
    > > column, at least I thought it should do this. It does in fact give me the
    > > warning in the column BUT it give me the warning if in the same row and this
    > > is permitted.
    > >
    > > How do I get the code correct so it just gives me the warning for the column
    > > values and not the row? Thanks
    > >
    > > Private Sub Worksheet_Change(ByVal Target As Range)
    > > 'Adjust next constant to your own needs
    > > Const myColumn As String = "B:AF"
    > > Dim rng As Range
    > > Dim Found As Range
    > >
    > > Set rng = UsedRange.Columns(myColumn)
    > > If Intersect(Target, rng) Is Nothing _
    > > Or Target.Value = "" _
    > > Then Exit Sub
    > > Set Found = rng.Find(Target.Value)
    > > If Found.Address <> Target.Address Then
    > > Target.Select
    > > MsgBox ("Duplicate code")
    > > End If
    > > End Sub


+ 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