Extracting similar words

Milade8080

New Member
Joined
Mar 13, 2014
Messages
25
Hi
Is there a way to make it possible to extract the same words between two sentences
For example, in cell A1 we have : My name is Morteza Ghovati
in cell B1 we have : Morteza Ghovati is My brother
That result is in cell C1 : My-is-Morteza-Ghovati
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

István Hirsch

Well-known Member
Joined
May 16, 2013
Messages
1,634
Are you interested in a formula solution where the common words are collected in C, as you requested, but some helper columns are also used right to column C?
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,893
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
For example, in cell A1 we have : My name is Morteza Ghovati
in cell B1 we have : Morteza Ghovati is My brother
That result is in cell C1 : My-is-Morteza-Ghovati
Give this UDF (user defined function) a try...
Code:
Function DupeWords(S1 As String, ByVal S2 As String) As String
  Dim X As Long, Words() As String
  Words = Split(S1)
  S2 = " " & S2 & " "
  For X = 0 To UBound(Words)
    If InStr(1, S2, " " & Words(X) & " ", vbTextCompare) Then
      DupeWords = DupeWords & "-" & Words(X)
    End If
  Next
  DupeWords = Mid(DupeWords, 2)
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use DupeWords just like it was a built-in Excel function. For example,

=DupeWords(A1,B1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 

Milade8080

New Member
Joined
Mar 13, 2014
Messages
25
Give this UDF (user defined function) a try...
Code:
Function DupeWords(S1 As String, ByVal S2 As String) As String
  Dim X As Long, Words() As String
  Words = Split(S1)
  S2 = " " & S2 & " "
  For X = 0 To UBound(Words)
    If InStr(1, S2, " " & Words(X) & " ", vbTextCompare) Then
      DupeWords = DupeWords & "-" & Words(X)
    End If
  Next
  DupeWords = Mid(DupeWords, 2)
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use DupeWords just like it was a built-in Excel function. For example,

=DupeWords(A1,B1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
Thanks for answer
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,651
Messages
5,838,579
Members
430,557
Latest member
MK15

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
Top