Prevent duplicate IF left string is repeating

NAP2012

New Member
Joined
Mar 14, 2019
Messages
10
C1-a
C1-b
C1prevent
C2
C2-aprevent

<tbody>
</tbody>









Hi,

I found many posts that have solution to prevent entering duplicate values in a column's cell.
I am trying to solve the same problem which has little different flavor.

In above table I want to stop users if they try to enter the same value (before "-").
E.G. A1-a if already there, A1 should not be allowed.

Essentially, the logic should first...
run this formula "=LEFT(C1,FIND("-",C1)-1)"
then run the list validation using countif.

I am using the folloiwng line of vb code but don't know how do i add the LEFT formula component to it or update the code to prevent duplicate comparing string before "-" i.e. dash.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.CountIf(Range("a:a"), Target) > 1 Then
    MsgBox "Duplicate....", vbCritical, "Can't take dups :("
    'what this should do after the error msg is closed
       Target.Value = ""
End If
End Sub
 
Hi Robert, this seems very near...except its allowing the same C18 in this example if I enter with dash.

Record #ID
1C10-a
2C10-b
3C10-c
4C10-d
5C11
6C12
7C13
8C14
9C15
10C16
11C17
12C18
13C18-a

<tbody>
</tbody>

row number 13 should have not been allowed as C18 exist before, so user need to start with next number or the one which does not exist without dash.
user can enter enter the same number again only if the same number exist with dash like C10-d, so I can enter C10-e.

see the further explanation i posted in the thread above.
 
Last edited:
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Maybe this:

Code:
Option Explicit
Option Compare Text 'Compare text but ignore case sensitivity
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim strMyKey As String
    Dim lngLastRow As Long
    Dim rngMyCell As Range
            
    If Target.Column = 1 And Len(Target.Value) > 0 Then
        If InStr(Target.Value, "-") > 0 Then
            strMyKey = Evaluate("LEFT(""" & Target.Value & """,FIND(""-"",""" & Target.Value & """)-1)")
        Else
            strMyKey = Target.Value
        End If
        lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
        For Each rngMyCell In Range("A1:A" & lngLastRow)
            If rngMyCell.Address <> Target.Address Then
                If Left(rngMyCell, Len(strMyKey)) = strMyKey Then
                    MsgBox Target.Value & " has already been entered." & vbNewLine & "As such cell " & Target.Address(False, False) & " will be cleared.", vbCritical
                    Application.EnableEvents = False
                        Target.ClearContents
                    Application.EnableEvents = True
                    Exit For
                End If
            End If
        Next rngMyCell
    End If

End Sub
 
Upvote 0
In post 11 you said not to allow C18-a so why is C10-e ok?
 
Upvote 0
Let me know how this goes and if it's wrong why:

Code:
Option Explicit
Option Compare Text 'Compare text but ignore case sensitivity
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim strMyKey As String
    Dim lngLastRow As Long
    Dim rngMyCell As Range
    Dim dblMyCount As Double
            
    If Target.Column = 1 And Len(Target.Value) > 0 Then
        lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
        If InStr(Target.Value, "-") > 0 Then
            dblMyCount = WorksheetFunction.CountIf(Range("A1:A" & lngLastRow), Target.Value)
            If dblMyCount = 1 Then
                Exit Sub
            ElseIf dblMyCount > 1 Then
                MsgBox Target.Value & " has already been entered." & vbNewLine & "As such cell " & Target.Address(False, False) & " will be cleared.", vbCritical
                Application.EnableEvents = False
                    Target.ClearContents
                Application.EnableEvents = True
                Exit Sub
            End If
        End If
        'If we get here the ID has been entered without a dash so check all entries against it to ensure it hasn't been used
        For Each rngMyCell In Range("A1:A" & lngLastRow)
            If rngMyCell.Address <> Target.Address Then
                If Left(rngMyCell, Len(Target.Value)) = Target.Value Then
                    MsgBox "To use """ & Target.Value & """ you will need to add a dash to it as well as the next letter for that range." & vbNewLine & "Cell " & Target.Address(False, False) & " will be cleared.", vbCritical
                    Application.EnableEvents = False
                        Target.ClearContents
                    Application.EnableEvents = True
                    Exit For
                End If
            End If
        Next rngMyCell
    End If

End Sub
 
Upvote 0
In post 11 you said not to allow C18-a so why is C10-e ok?

because C18 without dash exist. While C10 without dash does not exist.

your code is working all fine except its allowing dash entry for the same (C18 in example) number.

Record #Req.
C10-aexist
C10-bexist
C10-cexist
C10-dexist
C10should not allowed
C18exist
c18-ashould not allowed

<tbody>
</tbody>


To satisfy the requirement if I can rephrase to simplify...


  • If I am entering number with dash, (e.g. C18-a)
    • the same number without dash should not exist before (C18)
    • and exact same number with dash should also not exist before (C18-a)



  • If I am entering number without dash, (e.g. C10)
    • same number with dash should not exist before (C10-a/b/c...)
    • the exact same number (without dash) should not exist before (C10)
 
Last edited:
Upvote 0
OK - last attempt:

Code:
Option Explicit
Option Compare Text 'Compare text but ignore case sensitivity
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim strMyKey As String
    Dim lngLastRow As Long
    Dim rngMyCell As Range
    Dim dblMyCount As Double
    Dim rngMyRange As Range
    
    'Code built to only work on a single cell in Col. A
    'Adapted from here https://www.mrexcel.com/forum/excel-questions/651154-vba-worksheet_change-event-how-detect-if-multiple-cells-were-selected.html
    Set rngMyRange = Intersect(Target, Range("A:A"))
    If Not rngMyRange Is Nothing Then
        If rngMyRange.Cells.Count > 1 Then
            Exit Sub
        End If
    End If
            
    If Target.Column = 1 And Len(Target.Value) > 0 Then
        lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
        dblMyCount = WorksheetFunction.CountIf(Range("A1:A" & lngLastRow), Target.Value)
        'If the entry has been duplicated, then...
        If dblMyCount > 1 Then
            '...inform the user, clear the entry and quit
            MsgBox Target.Value & " has already been entered." & vbNewLine & "As such cell " & Target.Address(False, False) & " will be cleared.", vbCritical
            Application.EnableEvents = False
                Target.ClearContents
            Application.EnableEvents = True
            Exit Sub
        End If
        If InStr(Target.Value, "-") > 0 Then
            strMyKey = Evaluate("LEFT(""" & Target.Value & """,FIND(""-"",""" & Target.Value & """)-1)")
        Else
            strMyKey = Target.Value
        End If
        For Each rngMyCell In Range("A1:A" & lngLastRow)
            If rngMyCell.Address <> Target.Address Then
                If Len(rngMyCell) > 0 Then
                    If Left(rngMyCell.Value, Len(strMyKey)) = strMyKey Then
                        MsgBox "You cannot use " & Target.Value & " as " & strMyKey & " is already in use." & vbNewLine & "Cell " & Target.Address(False, False) & " will be cleared.", vbCritical
                        Application.EnableEvents = False
                            Target.ClearContents
                        Application.EnableEvents = True
                        Exit For
                    End If
                End If
            End If
        Next rngMyCell
    End If

End Sub

Robert
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,793
Messages
6,126,937
Members
449,349
Latest member
Omer Lutfu Neziroglu

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