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
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

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
36,334
Office Version
  1. 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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,436
Messages
5,528,749
Members
409,834
Latest member
vexceled

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top