2013-10-11 15 views
5

Jak mogę sprawdzić, czy dane z cmbTypeYacht.text już istnieją w cmbTypeYacht.list?Jak mogę sprawdzić, czy dane już istnieją na liście combobox?

Oto co mam:

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 

przykro znacznikiem im nie pewny co jest to jednak im przy użyciu Microsoft Visual Basic aplikacji.

+0

dobrze, co jest to, VB.NET lub VBScript? Lub VBA? Zamierzam założyć VB.NET, ale nie masz nawiasów ... – Ryan

Odpowiedz

9

Klasa ComboBox ma FindStringExact() metodę, która załatwi za Ciebie, na przykład:

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 

UPDATE:

można też po prostu pętli poprzez listy, a także, jak to:

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 
+0

Dzięki karl, jestem po prostu ciekawy, czy są jakieś inne sposoby przy użyciu klasy FindStringExact()? – blackmaler

+0

przejdzie przez listę? – sam092

+0

@ user2869223 - patrz 'UPDATE:' w mojej odpowiedzi. –

0

Nie trzeba wykonywać iteracji przez combobox.items. Items.Contains będzie już iterować po liście dla ciebie.

Wystarczy użyć:

If cmbTypeYacht.Items.Contains(cmbTypeYacht.Text) Then 
    MessageBox.Show("Found It") 
    Exit For 
End If 
0

Wyszukany: VBA sprawdzić, czy dane już istnieje na liście combobox?
ale vba nie ma powyższych właściwości.

Sub TestString() 
    Dim myString As String 
    Dim i As Long 
    Dim strFound As Boolean 


    'Just for test purposes 
    myString = "Apple" 


    strFound = False 
    With Me.ComboBox1 
     'Loop through combobox 
     For i = 0 To .ListCount - 1 
      If .List(i) = myString Then 
       strFound = True 
       Exit For 
      End If 
     Next i 
     'Check if we should add item 
     If Not strFound Then .AddItem (myString) 
    End With 
End Sub 

Stwierdzono to po wielu szukają w http://www.ozgrid.com/forum/showthread.php?t=187763 i faktycznie działa

0

Pracuję w programie Excel 2013 i nie ma FindStringExact lub .Items.Contains tak, żadna z tych są ważne. Nie ma również potrzeby powtarzania listy. To bardzo proste. Biorąc pod uwagę górny róg "MyUserForm" i ComboBox "MyComboBox",

Dim my_index As Integer 
my_index = -1 
my_index = MyUserForm.MyComboBox.ListIndex 
If my_index >= 0 Then 
    MsgBox "Item is in the list" 
Else 
    MsgBox "Item is NOT in the list" 
End If 

wyjaśnienia: Ustaw wskaźnik do wartości, która ListIndex nie wróci (jak -1). Następnie spróbuj uzyskać ListIndex. Jeśli bieżąca wartość .Value nie znajduje się na liście, nowa wartość nie zostanie podana do my_index i pozostanie -1.

Powiązane problemy