How to use keyword to tag every row automatically

KellyWills

New Member
Joined
Mar 16, 2022
Messages
18
Office Version
  1. 365
Platform
  1. MacOS
Hi All,

I have a large set of data that requires me to tag each row based on 2 column. I think there's possibility of using IF but I'm not sure how to joint 2 column. Here's my listing:

Screenshot 2022-04-25 at 9.23.21 PM.png


The colored column is my intended outcome. Is there a possibility to get this outcome by using excel functions?

Screenshot 2022-04-25 at 9.24.26 PM.png


Thank you in advance
 
I don't use a Mac but some methods that I might have used for this are not available on that platform so I think that I would approach it like this. Perhaps not ideal but the best I can see at the moment.

On the Factors sheet I would add a formula column as shown

KellyWills_1.xlsm
ABC
1FactorsKeywordWords
2AApple1
3AEpal1
4AApple Juice2
5BPineapple1
6CStarfruit1
7DJuice1
8EWater1
9EVery Hot Water3
Factors
Cell Formulas
RangeFormula
C2:C9C2=LEN(B2)-LEN(SUBSTITUTE(B2," ",""))+1


Then sort that table by the Words column - Descending - to look like this.
This sorting may well mean that the Factors in column A do not stay grouped together - eg the 'E' factors in this example

KellyWills_1.xlsm
ABC
1FactorsKeywordWords
2EVery Hot Water3
3AApple Juice2
4AApple1
5AEpal1
6BPineapple1
7CStarfruit1
8DJuice1
9EWater1
Factors
Cell Formulas
RangeFormula
C2:C9C2=LEN(B2)-LEN(SUBSTITUTE(B2," ",""))+1


Now use this UDF

VBA Code:
Function DoTags(rLookup As Range, rVals As Range) As String
  Dim a As Variant
  Dim sVals As String, CurrTags As String
  Dim i As Long
  
  sVals = " " & Join(Application.Index(rVals.Value, 1, 0), " | ") & " "
  a = rLookup.Value
  For i = 1 To UBound(a)
    If InStr(1, CurrTags & ",", " " & a(i, 1) & ",", 1) = 0 Then
      If InStr(1, sVals, " " & a(i, 2) & " ", 1) > 0 Then
        CurrTags = CurrTags & ", " & a(i, 1)
        sVals = Replace(sVals, " " & a(i, 2) & " ", " ", 1, -1, 1)
      End If
    End If
  Next i
  DoTags = Mid(CurrTags, 3)
End Function

KellyWills_1.xlsm
RST
1Expected tags outcome
2I love to eat appleI like to drink juiceA, D
3I love apple juiceI love pineappleA, B
4I like to drink waterI love starfruitC, E
5I like both apple and pineappleI like to drink waterA, B, E
Data
Cell Formulas
RangeFormula
T2:T5T2=DoTags(Factors!A$2:B$9,R2:S2)
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I don't use a Mac but some methods that I might have used for this are not available on that platform so I think that I would approach it like this. Perhaps not ideal but the best I can see at the moment.

On the Factors sheet I would add a formula column as shown

KellyWills_1.xlsm
ABC
1FactorsKeywordWords
2AApple1
3AEpal1
4AApple Juice2
5BPineapple1
6CStarfruit1
7DJuice1
8EWater1
9EVery Hot Water3
Factors
Cell Formulas
RangeFormula
C2:C9C2=LEN(B2)-LEN(SUBSTITUTE(B2," ",""))+1


Then sort that table by the Words column - Descending - to look like this.
This sorting may well mean that the Factors in column A do not stay grouped together - eg the 'E' factors in this example

KellyWills_1.xlsm
ABC
1FactorsKeywordWords
2EVery Hot Water3
3AApple Juice2
4AApple1
5AEpal1
6BPineapple1
7CStarfruit1
8DJuice1
9EWater1
Factors
Cell Formulas
RangeFormula
C2:C9C2=LEN(B2)-LEN(SUBSTITUTE(B2," ",""))+1


Now use this UDF

VBA Code:
Function DoTags(rLookup As Range, rVals As Range) As String
  Dim a As Variant
  Dim sVals As String, CurrTags As String
  Dim i As Long
 
  sVals = " " & Join(Application.Index(rVals.Value, 1, 0), " | ") & " "
  a = rLookup.Value
  For i = 1 To UBound(a)
    If InStr(1, CurrTags & ",", " " & a(i, 1) & ",", 1) = 0 Then
      If InStr(1, sVals, " " & a(i, 2) & " ", 1) > 0 Then
        CurrTags = CurrTags & ", " & a(i, 1)
        sVals = Replace(sVals, " " & a(i, 2) & " ", " ", 1, -1, 1)
      End If
    End If
  Next i
  DoTags = Mid(CurrTags, 3)
End Function

KellyWills_1.xlsm
RST
1Expected tags outcome
2I love to eat appleI like to drink juiceA, D
3I love apple juiceI love pineappleA, B
4I like to drink waterI love starfruitC, E
5I like both apple and pineappleI like to drink waterA, B, E
Data
Cell Formulas
RangeFormula
T2:T5T2=DoTags(Factors!A$2:B$9,R2:S2)
Do I need to change anything in the UDF if I place “Words” column in the first column instead of the third?

What is the purpose of sorting? Does it gives any impact in the result?

Thank you.
 
Upvote 0
Do I need to change anything in the UDF if I place “Words” column in the first column instead of the third?
No, this would require no change in the UDF. When you use the UDF in the worksheet though it would require a change. Instead of, say
=DoTags(Factors!A$2:B$9,R2:S2)
you would need to use something like
=DoTags(Factors!B$2:C$9,R2:S2)

What is the purpose of sorting? Does it gives any impact in the result?
Yes, it does impact the results. The purpose is to ensure that the longest multi-word phrases are search for first. If we don't do that with the method that I have used, then you might get a 'D' for 'juice' in "I love apple juice" instead of or as well as an 'A'. That's why I asked the question I did in post #19.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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