如何检查组合框列表中是否已存在数据?

bla*_*ler 6 vba

如何检查cmbTypeYacht.text已存在的数据cmbTypeYacht.list

这是我得到的:

Dim TypeYacht As String 'Type of yacht input

TypeYacht = cmbTypeYacht.Text

If TypeYacht = ("cmbTypeYacht list") Then
    MsgBox "Type of Yacht is already on the list", vbExclamation, "Yacht Chantering"
Else
    cmbTypeYacht.AddItem cmbTypeYacht.Text

    With cmbTypeYacht
        .Text = ""
        .SetFocus
    End With
End If
Run Code Online (Sandbox Code Playgroud)

抱歉标签我不太确定它是什么但我使用Microsoft Visual Basic应用程序.

Kar*_*son 9

ComboBox班有一个FindStringExact()会做的伎俩对你来说,这样的方法:

Dim resultIndex As Integer = -1

resultIndex = cmbTypeYacht.FindStringExact(cmbTypeYacht.Text)

If resultIndex > -1 Then
    ' Found text, do something here
    MessageBox.Show("Found It")
Else
    ' Did not find text, do something here
    MessageBox.Show("Did Not Find It")
End If
Run Code Online (Sandbox Code Playgroud)

您也可以循环浏览列表,如下所示:

Dim i As Integer = 0
For i = 0 To cmbTypeYacht.Items.Count - 1
    If cmbTypeYacht.Items.Contains(cmbTypeYacht.Text) Then
        MessageBox.Show("Found It")
        Exit For
    End If
Next
Run Code Online (Sandbox Code Playgroud)


SM1*_*77Y 5

我在 Excel 2013 中工作,并且没有 FindStringExact 或 .Items.Contains 所以,这两个都不是有效的。也不需要迭代列表。其实很简单。给定一个用户表单“MyUserForm”和一个组合框“MyComboBox”,

If MyUserForm.MyComboBox.ListIndex >= 0 Then
    MsgBox "Item is in the list"
Else
    MsgBox "Item is NOT in the list"
End If
Run Code Online (Sandbox Code Playgroud)

说明:如果所选项目不在列表中,.ListIndex 返回 -1。