VBA finding if value is in values

vba

Solution

You could write a helper function like this one:

Public Function FindValue(ByVal ValueToFind As Variant, ParamArray SearchIn() As Variant)

    Dim i As Integer

    For i = 0 To UBound(SearchIn)
        If SearchIn(i) = ValueToFind Then
            FindValue = True
            Exit Function
        End If
    Next

End Function

The second parameter (the `ParamArray`) is an array, so you can actually pass an indefinite number of parameters.

So you can just pass all your values to this function - the one you want to find first, and after that all the ones you want to search in:

Dim Found As Boolean

Found = FindValue(y, x1, x2, x3, xn)

Problem

I have a value `y`, and I want to find out if that value is inside this set of values : `x1, x2, ... xn`. I can do it like this: ``` if(y = x1 or y = x2 or .....) ``` But is there a nicer way? pseudocode: ``` if(y in (x1, x2, ...., xn)) ```

Original source