0

I want to consider these two strings for example equal: leve mur#1 and leve mur

I found Like and InStr but I'm not interested in the position. I just want to ignore what's after the hashtag.

Dim oHead As Long
oHead = wF.Match(LookingHead.Value, _
          Range("A1", Cells(1, _
          Range("A1").SpecialCells(xlCellTypeLastCell).Column)), False)

2 Answers 2

1

I don't know why you don't want to use Instr but that is what I would think of first to solve this particular problem. When I use Instr I never really care about where in the string the match was found. I just need to know if one string contains the other. In your case, you may have a smaller string and a larger string. The order might not be known at run time, so, just run Instr both ways to make sure you catch any case.

Sub Button1_Click()
   Dim Str1 As String
   Dim Str2 As String

   Str1 = "leve mur#1"
   Str2 = "leve mur"

   If Contains(Str1, Str2) Then
       MsgBox "They match"
   Else
       MsgBox "No match"
   End If
End Sub

Function Contains(Str1 As String, Str2 As String) As Boolean

   If Instr(Str1, Str2) > 0 Or Instr(Str2, Str1) > 0 Then
      Contains = True
   Else
      Contains = False
   End If

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

1 Comment

cuz i loop through an entire column looking for matches in another so i can have stuff like this : leve mur#1 leve mur #2 etc compared to leve mur or test#1 test#2 etc compared to test
0

Why not use:

if instr(1 str1, str2, vbTextCompare) > 0 then
    'str2 has been found in str1
end if

Using the same approach, if you want to ignore what is after the hashtag...

hashPosit = InStr(1, str1, "#", vbTextCompare)
if hashPosit > 0 then
    'We have a hash in this string, only take to the left
    subStr1 = left(str1,hashPosit)
else 
    'No hash exists, take all string
    subStr1 = str1
endif

if instr(1, subStr1, str2, vbTextCompare) > 0 then
    'str2 has been found in the portion of str1 to the left of the #
end if

Bit clunky I suppose, but you could fire it into a function and it'd be clean enough.

Comments

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.