下面是我的代码,它将给定的textToSearch与组合框中的项目进行比较,如果它不在那里,则会提示用户关于它,否则选择它。
答案 0 :(得分:1)
Public Sub comboBoxCheck(listMy As ComboBox, textToSearch As String, msgBoxText As String)
Dim i As Integer
If textToSearch = "" Then textToSearch = "NA" 'Empty string is equilaent to NOT AVAILABLE in my case
textToSearch = UCase(textToSearch)
For i = 0 To listMy.ListCount - 1
If listMy.List(i) = textToSearch Then
listMy.ListIndex = i
Exit Sub
End If
Next
'could add the text but In this case prompt the user what the text was, typically spelling mistake etc..
MsgBox ("You have some invalid entry for " & msgBoxText & "which has the value: " & textToSearch & "Please select correct value in the combobox and then press OK!")
End Sub