Test if a given value matches an element of the provided array


2012, Feb 01 edited
tags: VBA Array Array 


001 : Function isInArray(el As Variant, v As Variant) As Boolean
002 : 'Desc: Check if element el is in input array v
003 : 'Copyright Realce.net 2012
004 : On Error GoTo emng_isInArray
005 :     Dim bRes As Boolean
006 :     bRes = False
007 :     Dim i As Integer
008 :     If isArray(v) Then
009 :         For i = LBound(v) To UBound(v)
010 :             If (v(i) = el) Then
011 :                 bRes = True
012 :                 Exit For
013 :             End If
014 :         Next i
015 :         isInArray = bRes
016 :         Exit Function
017 :     Else
018 :     MsgBox "Error : Not an array ", vbOKOnly + vbExclamation, "Error isInArray function"
019 :     Exit Function
020 :     End If
021 : emng_isInArray:
022 :     MsgBox "Error : " + Err.Description, vbOKOnly + vbExclamation, "Error array_show function"
023 :     Exit Function
024 : End Function






Function allowing to sort in ascending or descending order the elements of the v array



Thanks to the invert array function, it's possible to enhance the sort function in order to allow not only ascending but also descending order.

To do so, we add a parameter, a flag fl_asc allowing to sort in ascending order in case it's set to True, in descending order if set to false. In this case we use the array_invert function before returning the resulting array to obtain the desired sorting(code lines 021-026).

Function array_sort(v, fl_asc)
'Desc: sort the elements of the input array v
'Copyright Realce.net 2012
On Error GoTo emng_array_sort
If isArray(v) Then
    Dim cnt As Boolean
    cnt = True
    If (UBound(v) > 1) Then
        Do While (cnt = True)
            cnt = False
            For i = LBound(v) To UBound(v) - 1
                If (v(i) > v(i + 1)) Then
                    tmp = v(i)
                    v(i) = v(i + 1)
                    v(i + 1) = tmp
                    cnt = True
                End If
            Next i
        Loop
    End If
    If (fl_asc = True) Then
        array_sort = v
    Else
        array_sort = array_invert(v)
    End If
Exit Function
Else
    MsgBox "Error : Not an array ", vbOKOnly + vbExclamation, "Error array_sort function"
    Exit Function
End If
emng_array_sort:
    MsgBox "Error : " + Err.Description, vbOKOnly + vbExclamation, "Error array_sort function"
    Exit Function
End Function



Test code n°1 :

	
Sub test_array_sort()
    Dim e As Variant
    e = Array(3, 6, 7, 4, 4, 1, 2)
    MsgBox array_show(array_sort(e, True))
End Sub


The above code will produce the display of a message box containing the following text :

	
Array(1,2,3,4,4,6,7)

Test code n°2 :

	
Sub test_array_sort()
    Dim e As Variant
    e = Array(3, 6, 7, 4, 4, 1, 2)
    MsgBox array_show(array_sort(e, False))
End Sub


The above code will produce the display of a message box containing the following text :

	
Array(7,6,4,4,3,2,1)

The fuel of coding inspiration:music, travel, photography and whatever drives creativity to code.

You might also like