简体   繁体   中英

If combobox1 includes an item in combobox2 list, then return error

I have a Userform that has 2 Comboboxes on it.

-- One acts as a drop down with team names ie: Team 1, Team 2, Team 3, -- The other combobox is used as a text input.

I would like a VBA code that, after pressing an action button, it checks the input box value, and cross references it with the list inside the first combobox.

-- If the value of the box is NOT in there, I want it to action a index match function that will change the list value on some page and reload the userform. -- if the value of the box IS there, I want it to prompt the user with a message box "Cannot change team, duplicate or something".

I tried variables, For each statements & list index. No luck so far.

Ive changed the code that much that I do not have any semi working examples, but I will add this (I know its broken) but I will try convey what I want to happen:

KEY: Combobox1 = Select_Team (drop-down) Combobox2 = Change_Team (input)

For Each item In Select_Team

If Select_Team.listindex = Change_Team.Value Then
MsgBox "Duplicate"

Else:
MsgBox "This was changed"

'(This code works, I just need to implement the IF function)
Evaluate("=INDEX('Teams'!B2:S21,2,MATCH(""" & Select_Team.Value & """,'Teams'!B3:S3,0))") = Me.Change_Team.Value

Unload Me
Edit_Teams.Show

I got it working but I could not get it to flag duplicates. I ended up having a combobox with:

Team 1
Team 1
Team 1
Team 4
Team 5
Team 6
Team 7

The teams are pulled from another page from cell values, So the items are "hardcoded" and therefore cannot be removed by listindex -1.

I want the index match function to change the team names (which it does) but only if the Change_Team value is not in the dropdown combobox.

I believe your mistake is at how you interpret ComboBox.ListIndex . This property returns an integer, not the value of the corresponding item. In fact in your code, it should be sufficient to use item as it is.

For Each item In Select_Team.List
    If item  = Change_Team.Value Then
        MsgBox "Duplicate"
        Exit Sub
    End If
Next item
MsgBox "This was changed"
Evaluate("=INDEX('Teams'!B2:S21,2,MATCH(""" & Select_Team.Value & """,'Teams'!B3:S3,0))") = Me.Change_Team.Value
Unload Me
Edit_Teams.Show

This modified version of your code should work if you have declared the variables correctly. Also, I do not know the entirety of your setup, but be careful with using ComboBox.Value versus ComboBox.Text.

Hope this works, good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM