Searching in list of strings in VBA

yeheya

Board Regular
Joined
May 20, 2004
Messages
162
Hello Everyone,

I have list of string say "Apple","Mango","One","India","USA" ... list goes on..

I have to check if the user input is present in this list.

I am looking something like:

IF strUserInput in ("Apple","Mango","One","India","USA") THEN
MSGBOX "Found"
ENDIF

What is the most efficient way to do this in VBA?

Regards,
Yeheya
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ExampleCode()
<SPAN style="color:#00007F">Dim</SPAN> FruitRange <SPAN style="color:#00007F">As</SPAN> Range
<SPAN style="color:#00007F">Dim</SPAN> FruitArray() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> Fruit <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> FruitLoop <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> SpecialFruit <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> FruitCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#007F00">'Load Fruits into array</SPAN>
<SPAN style="color:#00007F">Set</SPAN> FruitRange = Range("A1:A6")
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Fruit <SPAN style="color:#00007F">In</SPAN> FruitRange
    <SPAN style="color:#00007F">If</SPAN> Trim(Fruit.Value) <> "" <SPAN style="color:#00007F">Then</SPAN>
        FruitLoop = FruitLoop + 1
        <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> FruitArray(FruitLoop)
        FruitArray(FruitLoop) = Fruit.Value
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">Next</SPAN> Fruit
<SPAN style="color:#007F00">'Check Array for presence of Fruit</SPAN>
SpecialFruit = "Mango"
<SPAN style="color:#00007F">If</SPAN> FruitLoop <> 0 <SPAN style="color:#00007F">Then</SPAN>
<SPAN style="color:#00007F">For</SPAN> FruitLoop = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(FruitArray)
<SPAN style="color:#00007F">If</SPAN> FruitArray(FruitLoop) = SpecialFruit <SPAN style="color:#00007F">Then</SPAN> FruitCount = FruitCount + 1
<SPAN style="color:#00007F">Next</SPAN> FruitLoop
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
MsgBox "Found " & FruitCount & " " & SpecialFruit & "s!"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
I feel there should be some better way of doing this, which will be more efficient than this.

Experts any comments...

Regards,
Yeheya
 
Upvote 0
Please comment!!!

yeheya said:
I feel there should be some better way of doing this, which will be more efficient than this.

Experts any comments...

Regards,
Yeheya
 
Upvote 0

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top