Search within a ComboBox list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to determine if a given number is in a ComboBox list; if so I’ll
trigger an action.
Any chance to use IF statements?, Doing a For/Next loop seems not to capture
it.

Thanks in advance
 
Hi there LuisE

the code below is one way of doing it though it does use a for/next
loop, just as a point if you are searching for Myname but the combobox
value is myname is will not be found so you have to be searching for
the exact match to the value in the combobox.

Option Explicit
Dim i As Integer
Dim MyVar As String

Function CheckCboBox()

MyVar = "what i'm looking for"

For i = 0 To ComboBox1.ListCount - 1

If ComboBox1.List(i) = MyVar Then

'You can add your code here to use the variable i.e.

MsgBox "hello" & vbNewLine & "I was line " & i & _
" in the combobox"

Exit Function 'Add this to stop searching
'if a name is found

End If

Next i

MsgBox "I was not found boooo to me"

End Function

Hope this helps


Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top