Match cell that contain common text

nikhil0311

Board Regular
Joined
May 3, 2013
Messages
200
Office Version
  1. 2013
Platform
  1. Windows
I am trying to match column A & B. I am not looking for exact match. For example cell A4 & B4 here we have Korea as a common word. If found then output column which is column C should say yes else no.
Country 1Country 2Final Output
CCBSI-INTERNATIONAL HQ - INDIAINDIAYes
CCBSI-INTERNATIONAL HQ - JAPANNo
CCBSI-INTERNATIONAL HQ - KOREAKOREA REPUBLIC OFYes
CCBSI-INTERNATIONAL HQ - NEW ZEALANDNEW ZEALANDYes
CCBSI-INTERNATIONAL HQ - INDONESIAAUSTRALIANo
Any help would be much appreciated. Thanks all and stay safe
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If you go to the Message Board and scroll down to the Hall of Fame Winners and go into that, you will see an article called "Alan's UDFs for the Fuzzy Match problem". Have a read of that. I think it might be the answer what you want - if reading it doesn't fry your brain! It's a bit complicated.
 
Upvote 0
Korea as a common word. If found then output column which is column C should say yes else no
So what about these? Each has a common word so should they all be a 'yes' in column C?

20 04 02.xlsm
AB
2CCBSI-INTERNATIONAL HQ - NEW ZEALANDNEW CALEDONIA
3CCBSI-INTERNATIONAL HQ - SOUTH KOREANORTH KOREA
4CCBSI-INTERNATIONAL HQ - UNITED KINGDOMUNITED STATES OF AMERICA
5CCBSI-INTERNATIONAL HQ - SOUTH SUDANSUDAN
Common word
 
Upvote 0
So what about these? Each has a common word so should they all be a 'yes' in column C?

20 04 02.xlsm
AB
2CCBSI-INTERNATIONAL HQ - NEW ZEALANDNEW CALEDONIA
3CCBSI-INTERNATIONAL HQ - SOUTH KOREANORTH KOREA
4CCBSI-INTERNATIONAL HQ - UNITED KINGDOMUNITED STATES OF AMERICA
5CCBSI-INTERNATIONAL HQ - SOUTH SUDANSUDAN
Common word
Well it not as easy as i thought it would be. To answer your question, NO.
 
Upvote 0
But lest assume if the Answe is Yes, what formula can we use?
I would employ a user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

I have provided two udfs. Neither is perfect but perhaps the second one may be closer to what you want?

The first looks for any "word" after the final "-" in column A in column B
The second looks for the whole exact text after the final "-" in column A in column B

VBA Code:
Function MatchWord(sCCBSI As String, sCountry As String) As String
  Dim RX As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "\b(" & Replace(Trim(Split(sCCBSI, "-")(2)), " ", "|") & ")\b"
  MatchWord = "No"
  If RX.test(sCountry) Then MatchWord = "Yes"
End Function


Function MtchWrd(sCCBSI As String, sCountry As String) As String
  Dim RX As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "\b" & Trim(Split(sCCBSI, "-")(2)) & "\b"
  MtchWrd = "No"
  If RX.test(sCountry) Then MtchWrd = "Yes"
End Function

ikhil0311 2020-04-02 1.xlsm
ABCD
1Country 1Country 2MatchWordMtchWrd
2CCBSI-INTERNATIONAL HQ - INDIAINDIAYesYes
3CCBSI-INTERNATIONAL HQ - JAPANNoNo
4CCBSI-INTERNATIONAL HQ - KOREAKOREA REPUBLIC OFYesYes
5CCBSI-INTERNATIONAL HQ - NEW ZEALANDNEW ZEALANDYesYes
6CCBSI-INTERNATIONAL HQ - INDONESIAAUSTRALIANoNo
7CCBSI-INTERNATIONAL HQ - NEW ZEALANDNEW CALEDONIAYesNo
8CCBSI-INTERNATIONAL HQ - SOUTH KOREANORTH KOREAYesNo
9CCBSI-INTERNATIONAL HQ - UNITED KINGDOMUNITED STATES OF AMERICAYesNo
10CCBSI-INTERNATIONAL HQ - SOUTH SUDANSUDANYesNo
11CCBSI-INTERNATIONAL HQ - SUDANSOUTH SUDANYesYes
Sheet1
Cell Formulas
RangeFormula
C2:C11C2=MatchWord(A2,B2)
D2:D11D2=MtchWrd(A2,B2)
 
Upvote 0
I would employ a user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

I have provided two udfs. Neither is perfect but perhaps the second one may be closer to what you want?

The first looks for any "word" after the final "-" in column A in column B
The second looks for the whole exact text after the final "-" in column A in column B

VBA Code:
Function MatchWord(sCCBSI As String, sCountry As String) As String
  Dim RX As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "\b(" & Replace(Trim(Split(sCCBSI, "-")(2)), " ", "|") & ")\b"
  MatchWord = "No"
  If RX.test(sCountry) Then MatchWord = "Yes"
End Function


Function MtchWrd(sCCBSI As String, sCountry As String) As String
  Dim RX As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "\b" & Trim(Split(sCCBSI, "-")(2)) & "\b"
  MtchWrd = "No"
  If RX.test(sCountry) Then MtchWrd = "Yes"
End Function

ikhil0311 2020-04-02 1.xlsm
ABCD
1Country 1Country 2MatchWordMtchWrd
2CCBSI-INTERNATIONAL HQ - INDIAINDIAYesYes
3CCBSI-INTERNATIONAL HQ - JAPANNoNo
4CCBSI-INTERNATIONAL HQ - KOREAKOREA REPUBLIC OFYesYes
5CCBSI-INTERNATIONAL HQ - NEW ZEALANDNEW ZEALANDYesYes
6CCBSI-INTERNATIONAL HQ - INDONESIAAUSTRALIANoNo
7CCBSI-INTERNATIONAL HQ - NEW ZEALANDNEW CALEDONIAYesNo
8CCBSI-INTERNATIONAL HQ - SOUTH KOREANORTH KOREAYesNo
9CCBSI-INTERNATIONAL HQ - UNITED KINGDOMUNITED STATES OF AMERICAYesNo
10CCBSI-INTERNATIONAL HQ - SOUTH SUDANSUDANYesNo
11CCBSI-INTERNATIONAL HQ - SUDANSOUTH SUDANYesYes
Sheet1
Cell Formulas
RangeFormula
C2:C11C2=MatchWord(A2,B2)
D2:D11D2=MtchWrd(A2,B2)
Thanks a lot Peter. Appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,223,981
Messages
6,175,768
Members
452,668
Latest member
mrider123

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