Hoping someone can help, I'm having an issue with my code. I'm using an error handler so not able to clarify the error itself.
Sheet 1 -
This is the list that I am attempting to categorise with my worksheetfunction.search, as my data is sensitive I have decided to use my breakfast as the search words!
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]Phrase[/TD]
[TD]Food[/TD]
[/TR]
[TR]
[TD]I will have bacon for breakfast[/TD]
[TD]bacon[/TD]
[/TR]
[TR]
[TD]I will have sausages for breakfast[/TD]
[TD]sausages[/TD]
[/TR]
[TR]
[TD]I will have bacon and sausages for breakfast[/TD]
[TD]multiple[/TD]
[/TR]
</tbody>[/TABLE]
Sheet 2 -
This is my array.
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[/TR]
[TR]
[TD]Food[/TD]
[/TR]
[TR]
[TD]bacon[/TD]
[/TR]
[TR]
[TD]sausage[/TD]
[/TR]
</tbody>[/TABLE]
In short - The macro is designed to look at the string from Sheet 1, Column A (excluding heading obviously), and find a match via a worksheetfunction.search in Sheet 2, Column A. If both matches are positive it is to display "Multiple" as seen in my category column of sheet 1. If no match is found I will use "No match".
My issue - my first string "I will have bacon for breakfast" matches bacon, perfect. My 2nd string "I will have sausages for breakfast" finds no matches. My 3rd string "I will have bacon and sausages for breakfast" returns "multiple", again perfect.
My code -
Thanks in advance for any help!
Sheet 1 -
This is the list that I am attempting to categorise with my worksheetfunction.search, as my data is sensitive I have decided to use my breakfast as the search words!
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]Phrase[/TD]
[TD]Food[/TD]
[/TR]
[TR]
[TD]I will have bacon for breakfast[/TD]
[TD]bacon[/TD]
[/TR]
[TR]
[TD]I will have sausages for breakfast[/TD]
[TD]sausages[/TD]
[/TR]
[TR]
[TD]I will have bacon and sausages for breakfast[/TD]
[TD]multiple[/TD]
[/TR]
</tbody>[/TABLE]
Sheet 2 -
This is my array.
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[/TR]
[TR]
[TD]Food[/TD]
[/TR]
[TR]
[TD]bacon[/TD]
[/TR]
[TR]
[TD]sausage[/TD]
[/TR]
</tbody>[/TABLE]
In short - The macro is designed to look at the string from Sheet 1, Column A (excluding heading obviously), and find a match via a worksheetfunction.search in Sheet 2, Column A. If both matches are positive it is to display "Multiple" as seen in my category column of sheet 1. If no match is found I will use "No match".
My issue - my first string "I will have bacon for breakfast" matches bacon, perfect. My 2nd string "I will have sausages for breakfast" finds no matches. My 3rd string "I will have bacon and sausages for breakfast" returns "multiple", again perfect.
My code -
Code:
Sub srch()
Dim tbl As ListObject
Dim ws1 As Worksheet, ws2 As Worksheet
Dim tarStr As String, list() As String
Set ws1 = Worksheets("Data")
Set ws2 = Worksheets("Lookups")
Set tbl = ws2.ListObjects("Table1")
i = tbl.Range.Rows.Count - 1
ReDim list(i)
For j = 1 To i
list(j) = ws2.Range("A" & j + 1).Value
Next j
ws1.Activate
ws1.Range("A2").Activate
Do While ActiveCell.Value <> ""
tarStr = ActiveCell.Value
On Error Resume Next
For j = 1 To i
strSrch = WorksheetFunction.Search(list(j), tarStr, 1)
If Err = 0 Then
x = x + 1
cat = list(j)
End If
If j = i And x <> 1 And cat <> "" Then
cat = "multiple"
End If
If j = i And x <> 1 And cat = "" Then
cat = "no match"
End If
Next j
ActiveCell.Offset(1, 0).Activate
x = 0
cat = ""
Loop
End Sub
Thanks in advance for any help!