Search Array for part of a cell (Custom Function)

C_Rieker

New Member
Joined
Nov 22, 2020
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello all.

I have the need to search a range of cells (List of Categories) to match only part of a given cell (Description). When found, the value from the matching cell (List of Categories) is displayed as the output (Category).

I am running into a problem where it only works if there is a match in the first cell of the range. it may be that it is super late but i am struggling to find why the error occurs.

Here is my custom function
VBA Code:
Public Function SearchArr(StrToMatch As String, RngToSearch As Range)

    Dim c As Range

    For Each c In RngToSearch
        If IsError(WorksheetFunction.Search(c.Value, StrToMatch)) Then
            Else
                If IsNumeric(WorksheetFunction.Search(c.Value, StrToMatch)) Then
                    SearchArr = c.Value
                    Exit Function
                    
                    Else
                End If
        End If
    Next c

' -------------VBA I Previously tried and also failed ---------------
'    Dim i As Long
'
'    For i = LBound(RngToSearch) To UBound(RngToSearch)
'        If IsNumeric(WorksheetFunction.Search(RngToSearch(i).Value, StrToMatch)) Then
'            SearchArr = RngToSearch(i).Value
'            Exit Function
'            Else
'                If i = UBound(RngToSearch) Then
'                    Exit Function
'                End If
'        End If
'    Next

End Function

and here is the mini-spreadsheet
Budget.xlsm
ABCD
1DescriptionCategoryList of Categories
2XllCricket#VALUE!Tennis
3tt9Golf#VALUE!Cricket
4New Cricket#VALUE!Hockey
5LyyTennisTTennisBasket Ball
6Hockey#VALUE!Golf
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=SearchArr(A2,$D$2:$D$6)


Thanks heaps. Also note that i am open to suggestions if you think a custom formula is a stupid way to go about my problem :)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Personally I'd use the formula in col C
+Fluff 1.xlsm
ABCD
1DescriptionCategoryList of Categories
2XllCricketCricketCricketTennis
3tt9GolfGolfGolfCricket
4New CricketCricketCricketHockey
5LyyTennisTTennisTennisBasket Ball
6HockeyHockeyHockeyGolf
Test
Cell Formulas
RangeFormula
B2:B6B2=SearchArr(A2,$D$2:$D$6)
C2:C6C2=LOOKUP(2,1/(ISNUMBER(SEARCH($D$2:$D$6,A2))),$D$2:$D$6)


But for your UDF you need to use
VBA Code:
If IsError(Application.Search(c.Value, StrToMatch)) Then
 
Upvote 0
Alternatively use the VBA Instr function like
VBA Code:
    For Each c In RngToSearch
        If InStr(1, StrToMatch, c.Value, vbTextCompare) > 0 Then
           SearchArr = c.Value
           Exit Function
        End If
    Next c
 
Upvote 0
Solution
Alternatively use the VBA Instr function like
VBA Code:
    For Each c In RngToSearch
        If InStr(1, StrToMatch, c.Value, vbTextCompare) > 0 Then
           SearchArr = c.Value
           Exit Function
        End If
    Next c
This works perfectly thank you :)
The other solutions you posted also worked but i didnt fully understand how they worked haha.
 
Upvote 0
The other solutions you posted also worked but i didnt fully understand how they worked haha.
What about this standard worksheet function? (assumes at most one match from column D could occur in the column A text as per your sample)

21 04 03.xlsm
ABCD
1DescriptionCategoryList of Categories
2XllCricketCricketTennis
3tt9GolfGolfCricket
4New CricketCricketHockey
5LyyTennisTTennisBasket Ball
6HockeyHockeyGolf
Check List
Cell Formulas
RangeFormula
B2:B6B2=FILTER(D$2:D$6,ISNUMBER(SEARCH(D$2:D$6,A2)),"")
 
Upvote 0
What about this standard worksheet function? (assumes at most one match from column D could occur in the column A text as per your sample)

21 04 03.xlsm
ABCD
1DescriptionCategoryList of Categories
2XllCricketCricketTennis
3tt9GolfGolfCricket
4New CricketCricketHockey
5LyyTennisTTennisBasket Ball
6HockeyHockeyGolf
Check List
Cell Formulas
RangeFormula
B2:B6B2=FILTER(D$2:D$6,ISNUMBER(SEARCH(D$2:D$6,A2)),"")
I like the direction of this approach. Unfortunately in my full spreadsheet (40k rows) that needs filtering, some of the descriptions have 3 or 4 matches. For the purpose of this post i just grabbed a small sample to make my question a little clearer. It would have probably been easier for myself had it been only 1 match.
 
Upvote 0
some of the descriptions have 3 or 4 matches.
What result do you want returned when that happens?

Perhaps just one of the matches?

21 04 03.xlsm
ABCD
1DescriptionCategoryList of Categories
2XllCricket & GolfCricketTennis
3tt9Golf & CricketCricketCricket
4New CricketCricketHockey
5LyyTennisTGolf, CricketTennisBasket Ball
6HockeyHockeyGolf
Check List
Cell Formulas
RangeFormula
B2:B6B2=INDEX(FILTER(D$2:D$6,ISNUMBER(SEARCH(D$2:D$6,A2)),""),1)



Perhaps all of the matches like this?

21 04 03.xlsm
AB
1DescriptionCategory
2XllCricket & GolfCricket, Golf
3tt9Golf & CricketCricket, Golf
4New CricketCricket
5LyyTennisTGolf, CricketTennis, Cricket, Golf
6HockeyHockey
Check List (2)
Cell Formulas
RangeFormula
B2:B6B2=TEXTJOIN(", ",1,FILTER(D$2:D$6,ISNUMBER(SEARCH(D$2:D$6,A2)),""))



Perhaps all of the matches like this?

21 04 03.xlsm
ABCDEFGH
1DescriptionList of CategoriesCategory
2XllCricket & GolfTennisCricketGolf
3tt9Golf & CricketCricketCricketGolf
4New CricketHockeyCricket
5LyyTennisTGolf, CricketBasket BallTennisCricketGolf
6HockeyGolfHockey
Check List (3)
Cell Formulas
RangeFormula
E5:G5,E4,E6,E2:F3E2=TRANSPOSE(FILTER(C$2:C$6,ISNUMBER(SEARCH(C$2:C$6,A2)),""))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top