Home > Blockchain >  How to do if statement that prevents a combo value from being added to a list box when the list box
How to do if statement that prevents a combo value from being added to a list box when the list box

Time:11-21

I am having trouble finding a way if the combobox value is selected to add to the listbox but if the combobox value is already display in the listbox then a msgbox appears saying "this value is already in the listbox". I am trying to use an if statement then a for loop. For example, if I pick the letter d and then add it to the listbox it will but if I pick d again from the combo box then a message will acquire saying this value is already in the listbox and will not add the letter d again.

I believe I should use an if statement, but I don't know have to formulate it

Private Sub cmdplayer_Click()
    Dim ratio As Double
    Dim formatratio As String
    Dim name As String
    
    Me.listbox.ColumnCount = 2

    If cmbComboBox.Value = "" Then
        MsgBox "Please Select a Player"
        
    ElseIf cmbComboBox.Value = Me.listbox.List Then
        MsgBox cmbComboBox.Value & " has already been added to your team"
    
    Else
        name = Me.cmbComboBox.Column(0)
        Me.listbox.AddItem name
        ratio = Me.cmbComboBox.Column(3)
        formatratio = FormatNumber(ratio, 1)
        Me.listbox.List(listbox.ListCount - 1, 1) = formatratio
    
    End If

End Sub

CodePudding user response:

try this:

Private Sub cmbComboBox_Change()
    Dim duplicate As Boolean
    Dim person As String, lName As String
    
    person = Me.cmbComboBox.Column(0)
        
    duplicate = False
    For i = 0 To Me.listbox.ListCount - 1

        lName = Me.listbox.List(i)
        If lName = person Then
            duplicate = True
            MsgBox "This person is already part of the team."
            Exit For
        End If
    
    Next i
    
    If Not duplicate Then
        Me.listbox.AddItem person
    End If
    
End Sub
  • Related