Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Exact match of two text using instr

Tags:

excel

vba

How can i match exactly the two texts in InStr function ?

Right now my code is like below

If InStr(1, arrcallpara(i), strvarcalll) > 0 Then
  blncalling = True
End If

arrcallpara(i) contains value "1234" or "12345" or "1234567" and strvarcalll contains "1234567"

The "If" condition should be satisfied only when there is exact match between arrcallpara(i) and strvarcalll.

like image 371
Subbu Avatar asked Oct 31 '22 08:10

Subbu


2 Answers

Why use InStr ? You can easily check if 2 strings match by using the = .

Anyway, let's say that you are somehow forced to use it, I would check first if a string is contained in the second, then if the second is contained in the first. That should do it, but to be totally sure you could also check if the lengths are equal:

If InStr(1, arrcallpara(i), strvarcalll) <> 0 And InStr(1, strvarcalll, arrcallpara(i)) <> 0 And Len(arrcallpara(i)) = Len(strvarcalll)
like image 131
Noldor130884 Avatar answered Nov 15 '22 07:11

Noldor130884


You could use StrComp

If StrComp(arrcallpara(i), strvarcalll) = 0 Then

or just plain old =

If arrcallpara(i) = strvarcalll Then

But more than that, your If is unnecessary clutter. Just say:

blncalling = (arrcallpara(i) = strvarcalll)

or

blncalling = (StrComp(arrcallpara(i), strvarcalll) = 0)
like image 28
Jean-François Corbett Avatar answered Nov 15 '22 05:11

Jean-François Corbett