0

So I have

  Dim str1
  str1 = "Cat"
  Dim str2
  str2 = "concatenate"

I wanted to know is there a way I can match str1 with str2 and return positive non-zero number if there is match (case-insensitive) for str1 in str2 ?

2

3 Answers 3

3

For VBA, The instr is the way to go:

InStr(1, str2, str1, vbTextCompare)

The first part is where it starts looking, in this case the first character.

The second is the string in which the search is happening.

The third is the search parameter.

The fourth determines whether it looks at the text of the binary value. So for a case-insensitive we use vbtextcompare.

Sign up to request clarification or add additional context in comments.

5 Comments

will this work if str1 and str2 are arrays with strings and str1(i) compares for a match with all str2(j) ?
No. There are many ways to do it, loops and dictionaries, depending on what is being searched. But that is not what you asked.
which variable will return a positive value if match is found ?
The result of the function that is being called will result in a positive number if found and a zero if not. So for your example the result would be 4. You would create a variable and set it = to the function above.
To work with an array you have to loop in and compare each position.
0

Use the indexOf method:

str2.indexOf(str1)

4 Comments

will this work if str1 and str2 are arrays with strings and str(i) compares for a match with all str(j) ?
no, neither will any of the other answers. You'll need to loop.
Yeah sure I will have to loop but it will work for arrays too right
indexing an array returns the object at a particular location. If that object is a string, it will act like one because it is one.
0

Use instr or instrrev:

instr(str2, str1, 1)

See msdn for more info. This does exactly what you need.

1 Comment

I may be wrong but this is for vb not vba, which is what the OP asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.