Search Tools Links Login

Find if a value exists in an Array


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

About this post

Posted: 2019-08-24
By: BrianGillham
Viewed: 257 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This code originally appeared on AndreaVB.com, and has been republished here with the permission of Andrea Tincani.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.