Userform combobox to stop if duplicate (cobobox text)

Sabotage

Board Regular
Joined
Sep 19, 2013
Messages
58
Hello Wizards,

I thank you for any future help in advance. I have a userform with multiple comboboxes and some of them are used for the same purpose. By that I mean these comboboxes use the same dynamical named range as rowsource. I want to stop the user from duplicating a combobox twice, so I'm looking to loop through each comboboxes with the same name prefix (like: cb_01, cb_02...) and if the value/text of the active combobox already used in another, it will give an warning message and clear the active cbox. I'm thinking to put the procedure onto the exit event because I dont want it to check whilst typing into the active combobox. I hope it makes sense.

Please help me...

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Modify to suit your own needs

Code:
Private Sub cb1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Call Dupes
End Sub

Private Sub cb2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Call Dupes
End Sub
Private Sub cb3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Call Dupes
End Sub

Private Sub Dupes()
    Dim arr As Variant, cb As Variant, cBox As Variant
    arr = Array(cb1, cb2, cb3)
    For Each cb In arr
        For Each cBox In arr
            If cb.Name <> cBox.Name Then
                If cb <> "" And cb.Value = cBox.Value Then
                    MsgBox cb.Value & vbCr & "deleted", vbCritical, "Duplicates not permitted"
                    cBox.Value = ""
                    Exit Sub
                End If
            End If
        Next cBox
    Next cb
End Sub
 
Last edited:
Upvote 0
Yongle,

Dunno why but I could not reply to your solution multiple times. I hope this will work now with quoting you. Your code works like a charm! Thank you ever so much!

Modify to suit your own needs

Code:
Private Sub cb1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Call Dupes
End Sub

Private Sub cb2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Call Dupes
End Sub
Private Sub cb3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Call Dupes
End Sub

Private Sub Dupes()
    Dim arr As Variant, cb As Variant, cBox As Variant
    arr = Array(cb1, cb2, cb3)
    For Each cb In arr
        For Each cBox In arr
            If cb.Name <> cBox.Name Then
                If cb <> "" And cb.Value = cBox.Value Then
                    MsgBox cb.Value & vbCr & "deleted", vbCritical, "Duplicates not permitted"
                    cBox.Value = ""
                    Exit Sub
                End If
            End If
        Next cBox
    Next cb
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,037
Members
448,543
Latest member
MartinLarkin

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