Extract partial matches string(words) between 2 columns in Excel - excel

I have two columns like below, I need to extract a partial word that matches between the 2 columns and put this in a 3rd columns.
Name1 Name2
RED CURRY CHICKEN GREEN CURRY CHICKEN
BEEF WITH MINT LEAVES BEEF WITH BASIL LEAVES
SWEET AND SOUND PORK BACON AND EGG
FRIED RICE FRIED RICE
My expected result
Name1 Name2 Partial Matches
RED CURRY CHICKEN GREEN CURRY CHICKEN CHICKEN
BEEF WITH MINT LEAVES BEEF WITH BASIL LEAVES BEEF WITH LEAVES
SWEET AND SOUND PORK BACON AND EGG AND
FRIED RICE FRIED RICE FRIED RICE

To do this with a formula is inefficient; you need vba for a sub procedure or user defined function.
Function friedRice(str1 As String, str2 As String)
Dim w As Long, words As Variant, tmp As String
words = Split(str1, Chr(32))
For w = LBound(words) To UBound(words)
If Not IsError(Application.Match(words(w), Split(str2, Chr(32)), 0)) Then
tmp = tmp & Chr(32) & words(w)
End If
Next w
friedRice = Trim(tmp)
End Function

Related

Excel - matching multiple strings of two columns and turn true shared strings in third column

Example:
A1 B1
1 white dog black dog
2 big tall tall building
3 wood green wood
4 big house green wood
5 long way street
I want to turn C1 true if A1 has one string match or full string match of B1 else C1 turn false:
A1 B1 C1
1 white dog black dog true
2 big tall tall building true
3 wood green wood true
4 big house green wood false
5 long way street false
=SUMPRODUCT(0+ISNUMBER(SEARCH(" "&TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)*(ROW(INDEX(A:A,1):INDEX(A:A,1+LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1)+1,LEN(A1)))&" "," "&B1&" ")))>0
Regards

Indexing multiple columns in Excel, return neighboring column

I am working on an excel sheet that will help me with meal-planning. Currently, I have my data stored in a manner like demonstrated below:
**Monday**
Breakfast Bagel 1 Cream Cheese 1
Lunch Spinach 1 Eggs 4 Cream Cheese 1
Dinner Pork 1 Eggs 2
**Tuesday**
Breakfast Cereal 2 Milk 1 Eggs 3
Lunch Bagel 1 Butter 1
Dinner Eggs 2
CURRENT OUTPUT:
Ingredients Needed:
Eggs 2
Bagel 2
Spinach 1
Pork 1
Cereal 2
Milk 0
Butter 0
Cream Cheese 0
Where the numbers represent the number of servings of each ingredient I need to buy.
The problem I am facing is that my current formula:
=INDEX($C$12:$C$58, SMALL(IF(ISNUMBER(SEARCH($B$60, $B$12:$B$58)), MATCH(ROW($B$12:$B$58), ROW($B$12:$B$58))), ROW(B1)))
Only indexes one row at a time. For example, if I am searching for how many Eggs I need to buy for the week, the formula would return 2 eggs (because it is only searching column B for Eggs and returning column C).
Is there a work-around that would allow me to be able to return the full number of ingredients I need to buy? I realize I could index each column individually, however, since I have many ingredients (100+), using my current formula doesn't seem feasible.
Any help is massively appreciated
You can use SUMIF, where the sum range is offset from the criteria range. For example, for simplicity, let's say that C12 to H19 contains the data (ingredient and quantity), try...
=SUMIF($C$12:$G$19,B60,$D$12:$H$19)
...where B60 contains the ingredient of interest. Notice that the sum range D12:H19 is offset from the criteria range C12:G19.

Horizontal Index Match (returning column header)

A B C
1 Fruit Color Meat <- Column Header
2 Banana Red Pork
3 Apple Black Chicken
4 Orange White Beef
From the table1 above to table2 below:
A B
1 Name What? <- Column Header
2 Banana Fruit <- Formula should return these values, based on table 1
3 Red Color
4 Beef Meat
5 Pork Meat
Looking for a formula to return corresponding column name in B2,3,4...
I tried =INDEX(Table1[#Headers],MATCH(J:J,Table1,0))
It would seem that the three columns are unique; e.g. there would never be a Beef in the Color column. In that case you can simply query each column, passing back a 1, 2 or 3 as the case may be.
=IFERROR(INDEX(Table1[#Headers],
ISNUMBER(MATCH([#Name], Table1[Fruit], 0))*1+
ISNUMBER(MATCH([#Name], Table1[Color], 0))*2+
ISNUMBER(MATCH([#Name], Table1[Meat], 0))*3),
"no column")
    
I'm not sure whether Port was an intentional misspelling but it does demonstrate what occurs when there is no match.

Categorize single column of text into multiple columns

I am trying to create a macro that will categorize data in one column into multiple columns based on the item type. The data I am trying to categorize is a list of contracts with meta-data on the items in the contract.
The raw data looks like this:
Contract No Contract Name Item Type Item Description
111111 Chocolate Supplies POTS 5"
111111 Chocolate Supplies POTS 10"
111111 Chocolate Supplies POTS 15"
111111 Chocolate Supplies PANS 5"
111111 Chocolate Supplies PANS 10"
111111 Chocolate Supplies PANS 15"
111111 Chocolate Supplies KNIVES Paring knife
111111 Chocolate Supplies SILVERWARE Salad fork
111111 Chocolate Supplies SILVERWARE Dinner fork
111111 Chocolate Supplies SILVERWARE Dessert fork
111111 Chocolate Supplies SILVERWARE Dessert spoon
111111 Chocolate Supplies SILVERWARE Soup spoon
22222 Soups and Salads Order POTS 10"
22222 Soups and Salads Order POTS 15"
22222 Soups and Salads Order PANS 15"
22222 Soups and Salads Order KNIVES Butter knife
22222 Soups and Salads Order KNIVES Bread knife
22222 Soups and Salads Order KNIVES Paring knife
22222 Soups and Salads Order SILVERWARE Soup spoon
The final data needs to look like this (edited to include image):
Contract Contract Name POTS PANS KNIVES SILVERWARE
111111 Chocolate Supplies 5" 5" Paring knife Salad fork
10" 10" Dinner fork
15" 15" Dessert fork
Dessert spoon
Soup spoon
22222 Soups and Salads Order 10" 15" Butter knife Soup spoon
15" Bread knife
Paring knife
# What I've tried so far #
The current crude solution I am using is to:
- Run the query
- Paste the data into excel
- Create a pivot
- Use a series of count, offset and indirect formulas to reorganize the data as needed
- Since the above process leaves empty rows between each section of contracts, I copy-paste the data into a new worksheet, put an Autofilter and remove the blank rows
... and voila, that's the final report.
# Possible VBA solution #
I found this tutorial which seems to do exactly what I want, except for the problem where I need the macro to start a new section when the contract no. changes. I don't know how to get the VBA code below to also check for the contract no.
I'd love any help you could send my way. Thanks in advance!
# Code from tutorial on get-digital-help [dot] com by Oscar. #
This is not my code, and I give complete credit to Oscar's tutorial for getting me going in the right direction.
Sub Categorizedatatocolumns()
Dim rng As Range
Dim dest As Range
Dim vrb As Boolean
Dim i As Integer
Set rng = Sheets("Sheet1").Range("A4")
vrb = False
Do While rng <> ""
Set dest = Sheets("Sheet1").Range("A20")
Do While dest <> ""
If rng.Value = dest.Value Then
vrb = True
End If
Set dest = dest.Offset(0, 1)
Loop
If vrb = False Then
dest.Value = rng.Value
dest.Font.bold = True
End If
vrb = False
Set rng = rng.Offset(1, 0)
Loop
Set rng = Sheets("Sheet1").Range("A4")
Do While rng <> ""
Set dest = Sheets("Sheet1").Range("A20")
Do While dest <> ""
If rng.Value = dest.Value Then
i = 0
Do While dest <> ""
Set dest = dest.Offset(1, 0)
i = i + 1
Loop
Set rng = rng.Offset(0, 1)
dest.Value = rng.Value
Set rng = rng.Offset(0, -1)
Set dest = dest.Offset(-i, 0)
End If
Set dest = dest.Offset(0, 1)
Loop
Set rng = rng.Offset(1, 0)
Loop
End Sub
You may consider using pivot table which will give similar output.
Turn off the Subtotal and show data in tabular form for all fields.

Need help creating a user defined function in Excel with VBA

I'm totally stuck on creating a user defined function in Excel VBA for the following problem. Any help would be greatly appreciated.
My excel file looks like this (only adding a small portion for the sake of brevity):
A B C D
1 Susan Reagan Smith
2 Jill L Taylor
3 Sarah Sullivan Williams
4 Roger J Lopez
I would like a function that determines: If A1 = Susan OR Jill OR Sarah, AND LEN (length of string) of B1 >1 THEN D1 = A1 /2 B1 C1 ELSE A1 /2 C1
So the output in column D would look like:
A B C D
1 Susan Reagan Smith Susan /2 Reagan Smith
2 Jill L Taylor Jill /2 Taylor
3 Sarah Sullivan Williams Sarah /2 Sullivan Williams
4 Roger J Lopez Roger /2 Lopez
I want to use VBA because I have many names to add and don't really want a huge formula in cell D1 with a lot of nested if statements. I've been working on it myself but it's just a mess and I'm too embarrassed to post it here. Thanks guys!
Public Function JoinNames(A, B, C)
If FirstNameMatches(A) And Len(B) > 1 Then
JoinNames = A & " /2 " & B & " " & C
Else
JoinNames = A & " /2 " & C
End If
End Function
Private Function FirstNameMatches(N) As Boolean
Select Case N
Case "Susan", "Jill", "Sarah"
FirstNameMatches = True
End Select
End Function
Have a list of names somewhere else on a sheet.
Use IF(ISNA(MATCH(A1, list_of_names, 0)), "Not found", "Found") to figure out if the name is recognized.

Resources