Find a string in a combo box

To find out the listindex of an item in a combo box you need not loop thru all the items in the combo, instead you can do this very fast using SendMessage API.

'**Include the following lines in the declaration part

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Long, lParam As Any) As Long

Const CB_FINDSTRING = &H14C

'**Include the following code in any of the event from where you want to call SendMessage
Dim lReturnVal as long
Dim GivenStr as String

'Set some string which is in the Combo box
GivenStr = "Test"
lReturnVal = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal GivenStr)

In the above statement, if the function is successful 'lReturnVal' will return the listindex of the string found. So you can set the listindex property for the combo box to make the item selected.