devakotia
devakotia

Reputation: 101

function to determine if a specific string of characters exists in an array

Given an array, I would like to have a function which determines if a string of characters exists in an array. The string of characters must be EXACTLY the same in input of the function, and the array content.

Function IsInArray(ThsStrng As String, arr() As String, bnd As Integer) As Boolean
  For Z = 1 To bnd
    If arr(Z) = ThsStrng Then
      IsInArray = True
    Else
      IsInArray = False
    End If
  Next Z
End Function

At first, it seemed like the function ran correctly. After using this function a few times, I noticed that the False value (meaning that the input string does not equal to a value in the array), was not correct (there were input values that were exactly the same a value in the array).

Please help,

Upvotes: 0

Views: 45

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

Imagine what happens when the match is found in the middle of the array. The next iteration will be a mismatch, and the return value gets set back to False. You need to stop the loop when a match is found.

I believe the VBA syntax for that is

Exit For

Upvotes: 1

Related Questions