microsoft_excel:macro_search_binary
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
microsoft_excel:macro_search_binary [2020/07/15 09:30] – external edit 127.0.0.1 | microsoft_excel:macro_search_binary [2021/08/04 14:24] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Microsoft Excel - Macro Search Binary ====== | ||
- | |||
- | <code excel> | ||
- | ' Binary Search of array. | ||
- | ' Tries to find vCrit within oArrWithCrit. | ||
- | ' If found then return corresponding oArrWithValues otherwise vDefault. | ||
- | ' Note that due to way that binary search works this may not report on the very 1st instance of that vCrit if this | ||
- | ' was done sequentially. | ||
- | ' data against the same vCrit. | ||
- | Function ArrayFind(oArrWithCrit As Variant, vCrit As Variant, oArrWithValues As Variant, vDefault As Variant) | ||
- | Dim low As Long | ||
- | low = LBound(oArrWithCrit) | ||
- | Dim high As Long | ||
- | high = UBound(oArrWithCrit) | ||
- | Dim i As Long | ||
- | Dim result As Boolean | ||
- | ArrayFind = vDefault | ||
- | | ||
- | Do While low <= high | ||
- | i = (low + high) / 2 | ||
- | If vCrit = oArrWithCrit(i, | ||
- | ArrayFind = oArrWithValues(i, | ||
- | Exit Do | ||
- | ElseIf vCrit < oArrWithCrit(i, | ||
- | high = (i - 1) | ||
- | Else | ||
- | low = (i + 1) | ||
- | End If | ||
- | Loop | ||
- | | ||
- | End Function | ||
- | </ | ||
- | |||
- | and | ||
- | |||
- | <code excel> | ||
- | ' Tries to find vCrit within oArrWithCrit. | ||
- | ' If found then returns vFoundValue otherwise vNotFoundValue. | ||
- | ' Note that due to way that binary search works this may not report on the very 1st instance of that vCrit if this | ||
- | ' was done sequentially. | ||
- | ' data against the same vCrit. | ||
- | Function ArrayFindEx(oArrWithCrit As Variant, vCrit As Variant, vFoundValue As Variant, vNotFoundValue As Variant) | ||
- | Dim low As Long | ||
- | low = LBound(oArrWithCrit) | ||
- | Dim high As Long | ||
- | high = UBound(oArrWithCrit) | ||
- | Dim i As Long | ||
- | Dim result As Boolean | ||
- | ArrayFindEx = vNotFoundValue | ||
- | Do While low <= high | ||
- | i = (low + high) / 2 | ||
- | If vCrit = oArrWithCrit(i, | ||
- | ArrayFindEx = vFoundValue | ||
- | Exit Do | ||
- | ElseIf vCrit < oArrWithCrit(i, | ||
- | high = (i - 1) | ||
- | Else | ||
- | low = (i + 1) | ||
- | End If | ||
- | Loop | ||
- | | ||
- | End Function | ||
- | </ | ||
microsoft_excel/macro_search_binary.1594805433.txt.gz · Last modified: 2020/07/15 09:30 by 127.0.0.1