C_Rieker
New Member
- Joined
- Nov 22, 2020
- Messages
- 16
- Office Version
- 365
- Platform
- 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
and here is the mini-spreadsheet
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
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 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Description | Category | List of Categories | |||
2 | XllCricket | #VALUE! | Tennis | |||
3 | tt9Golf | #VALUE! | Cricket | |||
4 | New Cricket | #VALUE! | Hockey | |||
5 | LyyTennisT | Tennis | Basket Ball | |||
6 | Hockey | #VALUE! | Golf | |||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B2:B6 | B2 | =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