Check for duplicates

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a userform with 8 textboxes (txt_Name1, txt_Name2...txt_Name8)

I'm trying to add a piece of code to check the textboxes above if any name is duplicate, I tried the following code but didn't work

Can you please help me?


Code:
Private Sub cmd_Add_Clich()
Dim i As Long
For i = 1 To cmb_Num.value 'cmb_Num is a list (1 to 8)
If Me.Controls("txt_Name" & i) = Me.Controls("txt_Name" & i) Then
Me.lbl_Alert.Caption = "The name of User " & i & " is already exist!!!"
Me.Controls("txt_Name" & i).SetFocus
Exit Sub
End If
Next i
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about
Code:
Private Sub cmd_Add_Clich()
    Dim i As Long
    With CreateObject("scripting.dictionary")
        For i = 1 To cmb_Num.Value 'cmb_Num is a list (1 to 8)
            If .exists(Me.Controls("txt_Name" & i).Value) Then
                Me.lbl_Alert.Caption = "The name of User " & i & " is already exist!!!"
                Me.Controls("txt_Name" & i).SetFocus
                Exit Sub
            ElseIf Me.Controls("txt_Name" & i) <> "" Then
                .Add Me.Controls("txt_Name" & i).Value, Nothing
            End If
        Next i
    End With
End Sub
 
Upvote 0
If you want to add the check for empty text boxes, you can do it like
Code:
Private Sub cmd_Add_Clich()
    Dim i As Long
    With CreateObject("scripting.dictionary")
        [COLOR=#ff0000].CompareMode = 1[/COLOR]
        For i = 1 To cmb_Num.Value 'cmb_Num is a list (1 to 8)
            If Me.Controls("txt_Name" & i) = "" Then
                Me.lbl_Alert.Caption = "The name field for User" & i & "is empty"
                Me.Controls("txt_Name" & i).SetFocus
                Exit Sub
            ElseIf .exists(Me.Controls("txt_Name" & i).Value) Then
                Me.lbl_Alert.Caption = "The name of User " & i & " is already exist!!!"
                Me.Controls("txt_Name" & i).SetFocus
                Exit Sub
            Else
                .Add Me.Controls("txt_Name" & i).Value, Nothing
            End If
        Next i
    End With
End Sub
The part in red makes it case insensitive, if it needs to be case sensitive just remove it.
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,108
Members
452,544
Latest member
aush

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