User Tools

Site Tools


microsoft_excel:macros:search:search_sequentially

Microsoft Excel - Macros - Search - Search Sequentially

' Tries to find vCrit within oArrWithCrit.
' If found then return corresponding oArrWithValues otherwise vDefault.
Function ArrayFindSequential(oArrWithCrit As Variant, vCrit As Variant, oArrWithValues As Variant, vDefault As Variant)
 
    Dim vArr1 As Variant
    Dim vArr2 As Variant
    Dim lRow As Long
    ArrayFindSequential = vDefault
    If (UBound(oArrWithCrit) - LBound(oArrWithCrit)) = (UBound(oArrWithValues) - LBound(oArrWithValues)) Then
        For lRow = LBound(oArrWithCrit, 1) To UBound(oArrWithCrit, 1)
            If oArrWithCrit(lRow, 1) = vCrit Then
                ArrayFindSequential = oArrWithValues(lRow, 1)
                Exit Function
            End If
        Next
    Else
        ArrayFindSequential = "Criteriarange and sum range must be of same length"
    End If
 
 
    ' Clear all objects.
    Set vArr1 = Nothing
    Set vArr2 = Nothing
 
End Function

and

' Tries to find vCrit within oArrWithCrit.
' If found then returns vFoundValue otherwise vNotFoundValue.
'=ArrayFind(A1:A1000,"Foo",B1:B1000)
Function ArrayFindSequentialEx(oArrWithCrit As Variant, vCrit As Variant, vFoundValue As Variant, vNotFoundValue As Variant)
    Dim vArr1 As Variant
    Dim vArr2 As Variant
    Dim lRow As Long
    ArrayFindSequentialEx = vNotFoundValue
    For lRow = LBound(oArrWithCrit, 1) To UBound(oArrWithCrit, 1)
        If oArrWithCrit(lRow, 1) = vCrit Then
            ArrayFindSequentialEx = vFoundValue
            Exit Function
        End If
    Next
 
 
    ' Clear all objects.
    Set vArr1 = Nothing
    Set vArr2 = Nothing
 
End Function
microsoft_excel/macros/search/search_sequentially.txt · Last modified: 2021/08/04 14:43 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki