1
Dim string1 = "631;27;73"
Dim string2 = "31"

InStr(string1, string2) returns 2 since 31 is a subset of string1. For my current scenario, I want to match the whole number. string2 = 31 is not a matching number in string1 and hence I want to return a zero and append 31 to string1.

2
  • 2
    I see 2 ways you could approach this: a) split string1 at semicolons and compare the substrings with string2 in a loop, or b) use a regular expression where you match on word boundaries (\b). Commented Oct 17, 2019 at 15:37
  • Possible duplicate of Find substring from string output Commented Oct 17, 2019 at 17:32

2 Answers 2

2

You can split your number list into an array and compare each number:

Function NumberExists(p_sNumber, p_sList)
    Dim arrValues
    Dim iCounter

    arrValues = Split(p_sList, ";")

    For iCounter = 0 To UBound(arrValues)
        If p_sNumber = arrValues(iCounter) Then
            ' Number is found
            NumberExists = True
            Exit Function
        End If
    Next

    NumberExists = False

End Function

In your scenario, you can use the function like this:

Dim string1
Dim string2

string1 = "631;27;73"
string2 = "31"

MsgBox NumberExists(string2, string1)
Sign up to request clarification or add additional context in comments.

1 Comment

If this answer solved your problem, please accept the answer by clicking on gray check mark symbol on the left of an answer. A question with no accepted answer is rated as a not answered question even if there are good answers.
0

Try to Do This Code Get Better Result

Dim string1
Dim string2
Dim vCount

string1 = "631;27;73"
string2 = "632"
vCount = 1

vSplit = Split(string1, ";")

For each value1 in vSplit

If value1 = string2 And vCount = 1 Then
Result = "Number Exist"
vCount = vCount+1
ElseIf  vCount = 1 Then
Result = "Number Not Exist"
End If 

Next

MsgBox(Result)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.