Combobox duplicate validation

MFish

Board Regular
Joined
May 9, 2019
Messages
71
Hi there,

I have two comboboxes that will NEED to go into two separate columns. I need the code to run if the two comboboxes have both been picked exactly the same on the sheet before moving forward... I tried writing this code and it doesn't work with concatenating.

Code:
Range("B7:C7").Select
        'Check for duplicate bill name
Do Until UCase(ActiveCell) = UCase(cmbLH) & UCase(cmbLeg) Or ActiveCell = ""
        ActiveCell.Offset(1, 0).Select
Loop

If ActiveCell <> "" Then
        MsgBox "" & cmbLH & cmbLeg & " already exists in the Table. Please choose a different Line Haul Number", _
    vbOKOnly, "Duplicate Line Haul Number detected"
    frmAdd.Show
Exit Sub

Else: End If
Any thoughts on how I can run a duplicate validation on two cells next to eachother and if those match what I just have in the two comboboxes?
 
Last edited by a moderator:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,884
Office Version
2007
Platform
Windows
Try this


Code:
Private Sub CommandButton1_Click()
    'Validation
    Dim r As Range, f As Range, cell As String
    
    If cmbLH.ListIndex = -1 Or cmbLeg.ListIndex = -1 Then
        MsgBox "Select data"
        Exit Sub
    End If
    
    Set r = Range("B2", Range("B" & Rows.Count).End(xlUp))
    Set f = r.Find(cmbLH.Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not f Is Nothing Then
        cell = f.Address
        Do
            If f.Offset(, 1).Value = cmbLeg.Value Then
                MsgBox "" & cmbLH & " " & cmbLeg & ", already exists in the Table. " & _
                    "Please choose a different Line Haul Number", _
                    vbOKOnly, "Duplicate Line Haul Number detected"
            End If
            Set f = r.FindNext(f)
        Loop While Not f Is Nothing And f.Address <> cell
    End If
End Sub
 

Forum statistics

Threads
1,082,273
Messages
5,364,168
Members
400,785
Latest member
Mahar92

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top