Home » excel » excel – Search for an element in the VBA ArrayList

excel – Search for an element in the VBA ArrayList

Posted by: admin May 14, 2020 Leave a comment

Questions:

I hope you are great! I want to search through a VBA ArrayList and get the index number, the problem is, with For loop, you can only get the exact matches’ index. I have the most of my search element (highlighted in the red box) and I want to get the elements which highlighted in the blue box, is there any way to do this in VBA?

enter image description here

How to&Answers:

You can use the in-built function InStr to find an occurrence of one string inside another.

In your case change this:

If list(j) = search_element Then

To:

If InStr(1, list(j), search_element) > 0 Then

InStr returns the position of search_element within list(j). If the position is above 0 then the string was found. If it is 0 then nothing was found.

Therefore, this will be true if search_element occurs anywhere within list(j).

The documentation for InStr is here.