如何查看字符串是否与组合框列表中的项匹配

时间:2016-06-01 08:46:25

标签: vba excel-vba excel

下面是我的代码,它将给定的textToSearch与组合框中的项目进行比较,如果它不在那里,则会提示用户关于它,否则选择它。

1 个答案:

答案 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