AriKari
AriKari

Reputation: 323

how to find string in another string by using vba only

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 ?

Upvotes: 0

Views: 8716

Answers (3)

vacip
vacip

Reputation: 5426

Use instr or instrrev:

instr(str2, str1, 1)

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

Upvotes: 0

Scott Craner
Scott Craner

Reputation: 152605

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.

Upvotes: 3

ergonaut
ergonaut

Reputation: 7057

Use the indexOf method:

str2.indexOf(str1)

Upvotes: 0

Related Questions