Find if a value exists in an Array

By | 2019-08-24

This handy little function will determine if a value is present in an array, and return true or false.

Option Explicit

Public Function IsInArray(FindValue As Variant, arrSearch As Variant) As Boolean
   On Error GoTo LocalError
   If Not IsArray(arrSearch) Then Exit Function
   If Not IsNumeric(FindValue) Then FindValue = UCase(FindValue)
   IsInArray = InStr(1, vbNullChar & Join(arrSearch, vbNullChar) & vbNullChar, _
vbNullChar & FindValue & vbNullChar) > 0
Exit Function
LocalError:
   'Justin (just in case)
End Function

Usage

Private Sub Command1_Click()
   Dim x(5) As String
   
   x(0) = 5
   x(1) = 100
   x(2) = 2000
   x(3) = 11
   x(4) = 7
   x(5) = 1010
   MsgBox IsInArray(10, x)
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.