r/excelevator • Excel • Sep 14 '20

UDF - MULTIFIND ( find_range , words_range [, case_match , match_all , return_all ] ) - return a string/s from multiple search words

UDF - MULTIFIND ( find_range , words_range [, case_match , match_all , return_all ] )

MULTIFIND allows for the return of a string or multiple strings from a search list, matched from a list of words the user enters as criteria.


The advent of Dynamic Ranges in Excel 365 gives the opportunity to write UDFs that can return multiple array results in an easy manner where they Spill automatically; in this UDF with the return_all option set in the last argument.

The other option for pre Dynamic Ranges is to select a range of cells and enter the formula as an array formula with ctrl+shift+enter.. but this was written with 365 and dynamic ranges in mind.


find_range - the contiguous range of cells with the values to search in for a match

words_range - the contiguous range of cells with the search words in , one per cell.

case_match can be set to TRUE to return case matched values. MULTIFIND is not case sensitive by default.

match_all can be set to TRUE to only return a matched value when all the search words have been entered into the word_range. By default MULTIFIND starts to return matched values on the first search word as entered, returning more filtered values with each additional word,

return_all can be set to TRUE to return all matched values in an array. By default MULTIFIND will return the first matched result.

Examples

Source listExample 4 word search list range1Example 4 word Search list range2
An apple a daySNOWgarden
A banana go awaypeasmy
Snow Peas For Wintercarrots
Carrots in my gardenin
Lots of snow peas in winter
Carrots planted in my garden
Who planted carrots in my garden
Runner beans be running
FormulaExample 1 resultsWhy
=MULTIFIND(A2:A9,B2:B5)Snow Peas For Winterdefault
=MULTIFIND(A2:A9,B2:B5,1)#VALUE!case senstive
=MULTIFIND(A2:A9,B2:B5,,1)#VALUE!match all words
=MULTIFIND(A2:A9,B2:B5,,,1)Snow Peas For Winterarray
arrayLots of snow peas in winterarray
FormulaExample 2 resultsWhy
=MULTIFIND(A2:A9,C2:C5)Carrots in my gardendefault
=MULTIFIND(A2:A9,C2:C5,1)Who planted carrots in my gardencase matched
=MULTIFIND(A2:A9,C2:C5,0,1,1)Carrots in my gardenmatch all words, return all words
arrayCarrots planted in my garden
arrayWho planted carrots in my garden

Paste the following code into a worksheet module for it to be available for use.

Function MULTIFIND(t As Range, f As Range, Optional cSen As Boolean, Optional fAll As Boolean, Optional rAll As Boolean) As Variant
'MULTIFIND ( find_range , words_range [, case_match , match_all , return_all ] )
'https://www.reddit.com/u/excelevator
'https://old.reddit.com/r/excelevator
'https://www.reddit.com/r/excel - for all your Spreadsheet questions!
Dim tArray() As Variant
tArray = t
Dim fArray() As Variant
fArray = f
Dim aString As String
Dim fsize As Double
fsize = UBound(fArray)
Dim wCount As Integer
Dim tValue As String
Dim ta As Variant, fa As Variant
If Trim(Join(WorksheetFunction.Transpose(fArray))) = "" Then Error (13)
For Each ta In tArray
wCount = 0
tValue = IIf(cSen, ta, UCase(ta))
    For Each fa In fArray
        If Not cSen Then fa = UCase(fa)
        If InStr(tValue, fa) And IIf(fAll, fa <> "", True) Then wCount = wCount + 1
    Next
    If wCount = fsize Then aString = aString & ta & "§": If Not rAll Then Exit For
Next
MULTIFIND = WorksheetFunction.Transpose(Split(Left(aString, Len(aString) - 1), "§"))
End Function

Let me know if you find any bugs!


See a whole bundle of other custom functions at r/Excelevator

11 Upvotes

5 comments sorted by

3

u/vbahero Sep 14 '20

Great stuff. I really don't understand why MS doesn't go to town building this sort of productivity function natively into Excel, but oh well

Does this work if the ranges are horizontal rather than vertical? I don't have 365 to be able to test it, but in my experience I always had to check if .Columns or .Rows was longer / size = 1 and modify my calls to WorksheetFunction.Transpose at runtime accordingly (i.e. sometimes needed to call it twice, other times didn't need to call it at all depending on the UDF)

2

u/excelevator Excel Sep 15 '20

It was designed for Tables of data. Users can transpose their data with the TRANSPOSE function.

As you have experienced, trying to work with vertical and horizontal arrays can get very messy very quickly with all the validation logic thrown in... though I have included that in my other UDFs where appropriate.

1

u/monxstar Sep 15 '20

Oh hey! An improved function of what I'm currently doing. Thanks!

1

u/monxstar Sep 15 '20

For an improvement, I was thinking, is it better to have users input in a box or is it just more work for the user?

3

u/excelevator Excel Sep 15 '20

That is outside the remit of a worksheet function. A worksheet function only acts on the cell input values.

You can pretty format the cell range area used for the search words though or have a Table for the search words and use the Table reference in the formula; that way as you add more search words the formula dynamically incorporates those words.