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
 
Here is another function that you could try.

VBA Code:
Function TagsAdd(rLookup As Range, rVals As Range) As String
  Dim varLookup As Variant
  Dim c As Range
  Dim s As String
  Dim i As Long
  
  varLookup = rLookup.Value
  For Each c In rVals
    s = s & " " & c.Value
  Next c
  s = s & " "
  For i = 1 To UBound(varLookup)
    If Len(varLookup(i, 2)) > 0 Then
      If InStr(1, s, " " & varLookup(i, 2) & " ", vbTextCompare) > 0 Then
        If InStr(1, TagsAdd & ",", ", " & varLookup(i, 1) & ",", vbTextCompare) = 0 Then TagsAdd = TagsAdd & ", " & varLookup(i, 1)
      End If
    End If
  Next i
  TagsAdd = Mid(TagsAdd, 3)
End Function

My 'Tags' sheet is the same as in the last post.

'Data' sheet

KellyWills.xlsm
RST
1
2I love to eat appleI like to drink juiceA, D
3I like pineapple 
4I love starfruitB
5I Like to eat appleI like to drink waterA, E
6I love strawberryI love milkC, F
7Horses like strawBerry is nice 
8I like juiceTom likes milk & juiceD, F
Data
Cell Formulas
RangeFormula
T2:T8T2=TagsAdd(Tags!A$2:B$12,R2:S2)
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Having the data on two sheets should not be a problem. The question is what are the sheets and where is the data on them?
Your formula above indicates that the lookup table of tags/fruit is in columns A:B of sheet 'Tags' but post #1 indicates they are in columns A:B of sheet 'Listing'.
The above formula indicates the values to look in are in columns R:S of the sheet that the formula is on, post 1 has them in A:B

So, here is my sample workbook as I think you have it based on the post 9 formula.

Sheet Tags

KellyWills.xlsm
AB
1Tags
2AApple
3BStarfruit
4CStrawberry
5DJuice
6EWater
7FMilk
8
9
10
11
12
Tags


Sheet Data (but the name of this sheet is irrelevant)

KellyWills.xlsm
RST
1
2I love to eat appleI like to drink juiceA, D
3I like pineapple 
4I love starfruitB
5I Like to eat appleI like to drink waterA, E
6I love strawberryI love milkC, F
7Horses like strawBerry is nice 
8I like juiceTom likes milk & juiceD, F
Data
Cell Formulas
RangeFormula
T2:T8T2=AddTags(Tags!$A$2:$B$12,R2:S2)


If all of that seems correct and it is still not working for you then one possibility might be a Windows v Mac issue. I don't use a Mac so I cannot test in that environment. I am also not particularly familiar with Windows/Mac issues.

Can you confirm that the formula is resulting in a blank cell and no error message is received?
Hi Peter,

I have tested out in Windows but it shows me #VALUE! error instead of blanks like in Mac. From your code I did change these item in green according to my column name.
Rich (BB code):
Function AddTags(rLookup As Range, rVals As Range) As String
  Dim Foods As Variant, Tags As Variant, itm As Variant
  Dim sTag As String
  Dim pos As Variant
 
  Tags = Application.Transpose(rLookup.Columns(1).Value)
  Foods = Application.Transpose(rLookup.Columns(2).Value)
  For Each itm In Split(Join(Application.Index(rVals.Value, 1, 0)))
    pos = Application.Match(itm, Foods, 0)
    If IsNumeric(pos) Then
      sTag = Tags(pos)
      If InStr(1, AddTags & ",", ", " & sTag & ",", vbTextCompare) = 0 Then AddTags = AddTags & ", " & sTag
    End If
  Next itm
  AddTags = Mid(AddTags, 3)
End Function
 
Last edited by a moderator:
Upvote 0
From your code I did change these item in green according to my column name.
If you want to format vba code your own way in the forum (eg green) then you need to use the "RICH" code tags, not the "VBA" code tags. I have changed that for you this time.

What did you change them to? Can you post the actual code that you used?

Can you also post some small sample data (both sheets) like in post #1 so the we can see the the sheet names and what rows/columns the data is in?
When you do the sheet with the actual formula, select the first formula cell so that we can see the formula in the formula bar.
 
Upvote 0
If you want to format vba code your own way in the forum (eg green) then you need to use the "RICH" code tags, not the "VBA" code tags. I have changed that for you this time.

What did you change them to? Can you post the actual code that you used?

Can you also post some small sample data (both sheets) like in post #1 so the we can see the the sheet names and what rows/columns the data is in?
When you do the sheet with the actual formula, select the first formula cell so that we can see the formula in the formula bar.
Hi Peter,

Thank you for changing it to RICH, I didn't know. I have successfully altered your code to fit my data. Here's the code that I'm using
VBA Code:
Function AddTags(rLookup As Range, rVals As Range) As String
  Dim Keyword As Variant, Tag As Variant, itm As Variant
  Dim sTag As String
  Dim pos As Variant
  
  Tag = Application.Transpose(rLookup.Columns(1).Value)
  Keyword = Application.Transpose(rLookup.Columns(2).Value)
  For Each itm In Split(Join(Application.Index(rVals.Value, 1, 0)))
    pos = Application.Match(itm, Keyword, 0)
    If IsNumeric(pos) Then
      sTag = Tag(pos)
      If InStr(1, AddTags & ",", ", " & sTag & ",", vbTextCompare) = 0 Then AddTags = AddTags & ", " & sTag
    End If
  Next itm
  AddTags = Mid(AddTags, 3)
End Function

I works fine on my Mac. However, I noticed there are some rows are not tagged according to Tag list. For example, referring to your post #10, row 2 shows the tag in column T but if I have the same exact data in row 550, column T returns blank instead of A, D tag.
 
Upvote 0
I have not been able to reproduce that problem. I am now using the code from post #14 and here are my sheets

KellyWills.xlsm
AB
1Tags
2AApple
3BStarfruit
4CStrawberry
5DJuice
6EWater
7FMilk
8
Tags


KellyWills.xlsm
RST
1
2I love to eat appleI like to drink juiceA, D
3I like pineapple 
4I love starfruitB
5I Like to eat appleI like to drink waterA, E
6I love strawberryI love milkC, F
7Horses like strawBerry is nice 
8I like juiceTom likes milk & juiceD, F
9 
549 
550I love to eat appleI like to drink juiceA, D
Data
Cell Formulas
RangeFormula
T2:T9,T549:T550T2=AddTags(Tags!$A$2:$B$12,R2:S2)
 
Upvote 0
I have not been able to reproduce that problem. I am now using the code from post #14 and here are my sheets

KellyWills.xlsm
AB
1Tags
2AApple
3BStarfruit
4CStrawberry
5DJuice
6EWater
7FMilk
8
Tags


KellyWills.xlsm
RST
1
2I love to eat appleI like to drink juiceA, D
3I like pineapple 
4I love starfruitB
5I Like to eat appleI like to drink waterA, E
6I love strawberryI love milkC, F
7Horses like strawBerry is nice 
8I like juiceTom likes milk & juiceD, F
9 
549 
550I love to eat appleI like to drink juiceA, D
Data
Cell Formulas
RangeFormula
T2:T9,T549:T550T2=AddTags(Tags!$A$2:$B$12,R2:S2)
Hi Peter,

I think I figured why I have the error. For example in your post #15, if I change the column B in Tag sheet into more than 2 words, it won't work. For example, instead of just Apple, the keyword is Apple Juice. I'm not sure where in the code should I change this. My actual data of keyword may be a single to 3 words.

Thank you.
 
Upvote 0
My actual data of keyword may be a single to 3 words.
Then that is quite a different scenario. ;)

Could we have a new small but representative set of sample data and expected result (with XL2BB so that we can easily copy for testing)?
 
Upvote 0
Then that is quite a different scenario. ;)

Could we have a new small but representative set of sample data and expected result (with XL2BB so that we can easily copy for testing)?
I see, this is my first trying to use it. Hopefully it is correct.

Kelly Sample.xlsx
AB
1FactorsKeyword
2AApple
3AEpal
4AApple Juice
5BPineapple
6CStarfruit
7DJuice
8EWater
Factors


The reason why there's 3 A is because Apple & Epal can be tag as the same category.

Kelly Sample.xlsx
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
 
Upvote 0
Thanks for the sample. Just to be sure: Can you confirm that row 3 does not also get a 'D' tag even though it contains the word "juice"?
 
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