ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Search within a ComboBox list (https://www.excelbanter.com/excel-programming/401230-search-within-combobox-list.html)

LuisE

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

Thanks in advance



Incidental

Search within a ComboBox list
 
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





LuisE

Search within a ComboBox list
 
Thanks

"Incidental" wrote:

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







All times are GMT +1. The time now is 11:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com