Find list of words from sheet2 in sheet1 before a comma and extract text vba

satish78

Board Regular
Joined
Aug 31, 2014
Messages
218
Hi Friends,

Trying to find the solution on my task. But did not find suitable one to the need.
Here is my query and sample file with details.

Need a formula to find list of words before a (,) comma from sheet2A:A in Sheet1A:A(there is no data range like A1:a10) and extract text before the match
Formula should out put results like shown in ColumnD
ColumnC has found words from sheet2A:A in sheet1A:A

 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
this is what you want? (green table)
extract.png

if so, use Power Query (Get&Transform)
Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ETBD = Table.TransformColumns(Source, {{"RAW", each Text.BeforeDelimiter(_, ",", {0, RelativePosition.FromEnd}), type text}}),
    ITBD = Table.AddColumn(ETBD, "Text Before Delimiter", each Text.BeforeDelimiter([RAW], " "&[keywords], {0, RelativePosition.FromEnd}), type text),
    ROC = Table.SelectColumns(ITBD,{"Text Before Delimiter"})
in
    ROC
 
Last edited:
Upvote 0
a) is the correct answer.
If a) is the correct answer then there is no point having "Close to" in the list in the first place.
So, actually, you have all the following entries in Sheet 2 that are not required as they are multiple words where the last word is already in the list. They won't really hurt being there but it does introduce more processing that needed.

Book1
P
1According to
2Ahead of
3Along with
4Apart from
5As per
6As to
7As well as
8Aside from
9Away from
10Because of
11By means of
12Close to
13Contrary to
14Depending on
15Due to
16Forward of
17Further to
18In addition to
19In between
20In case of
21In favor of
22In front of
23In lieu of
24In spite of
25In the face of
26In view of
27Instead of
28Irrespective of
29Near to
30Next to
31On account of
32On behalf of
33On to
34On top of
35Opposite to
36Other than
37Out of
38Outside of
39Owing to
40Preparatory to
41Prior to
42Regardless of
43Thanks to
44Together with
45Up against
46Up to
47Up until
48With reference to
49With regard to
Sheet1
 
Upvote 0
a) is the correct answer.
In that case, try this user-defined function.

VBA Code:
Function BeforeComma(s As String, rWords As Range) As String
  Static RX As Object
  Dim Bits As Variant
  Dim tmp As String
  Dim i As Long
  
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.IgnoreCase = True
  End If
  RX.Pattern = "^\b(" & Join(Application.Transpose(rWords), "|") & ")\b[^#]*\,"
  Bits = Split(s)
  For i = UBound(Bits) To 0 Step -1
    tmp = Bits(i) & " " & tmp
    If RX.test(tmp) Then
      BeforeComma = Trim(Left(s, Len(s) - Len(tmp)))
      Exit For
    End If
  Next i
End Function

Here is the sample data and results (using the original list in column A of Sheet2 of the sample file that you provided).

Book1
AB
1 Coordinated team in bringing about Business Requirements, Architectural Specifications, CorporateCoordinated team in bringing
2Major accomplishment: The data transfer from about 95 databases, are continuously loaded into one database in less than 15 minutes, after numerous aggregations and calculations.Major accomplishment: The data transfer from about 95 databases, are continuously loaded into one database in less
3Pre-screened and pre-counseled potential applicants; provided social and academic information about college life and experiences; guided campus tours; spoke on student panels; contacted, arranged, and visited high schools to represent and promote Colorado State University.Pre-screened and pre-counseled potential applicants; provided social and academic information about college life and experiences; guided campus tours; spoke
4 · Hobbies: Reading about grid computing, computer forensics and data analysis, Arts· Hobbies: Reading
5College Editor Revamped and elevated quality of alumnae magazine by giving marketing-focused publication a journalistic slant through the telling of vibrant stories about the students, alumnae, and faculty.College Editor Revamped and elevated quality of alumnae magazine by giving marketing-focused publication a journalistic slant through the telling of vibrant stories
6ï‚·Developed a smart rule engine for CAS that tracks, alerts, emails and generates Crystal/Jasper reports for decision-makers in a company about any customer-employee issue whenever the rule is applicable. 
7Administrator Module to create, edits, delete and enquire about the registeredAdministrator Module
8 about 3,000 bank branches in some two dozen western and mid western states, in addition toabout 3,000 bank branches
9 information about the visitor's behavior, tracking campaign effectively and see how visitors found the site, howinformation
10 and brand identity; marketing, cross-selling of products, as well as building customer awareness about the organization and its services.and brand identity; marketing, cross-selling
11 Regular Interaction with the client team appraising them about project status, risks, challenges & resourceRegular Interaction with the client team appraising them
12 Regular Interaction with the client team appraising them about project status, risks, challenges & resourceRegular Interaction with the client team appraising them
13Knowledge about different process such as Negative news check, transaction settlement ,Knowledge about different process such
14 5% of revenue to about 30% in 4 years, improved service quality, shortened time to market for new offerings5% of revenue to about 30%
15 Gained knowledge about networking protocols (IPv6, TCP), IP Networking Infrastructure and itsGained knowledge
16 Gained knowledge about the North American digital hierarchy including DS0, DS1, DS3 andGained knowledge about the North American digital hierarchy
17 social network in planning, budgeting, shopping and communicating about her weddingsocial network
18 for a beef plant with an annual revenue of about $600,000,000.for a beef plant with an annual revenue of
19 of educating them about technologies to increase business productivity, save time, and meetof educating them about technologies to increase business productivity,
20 traffic flow for the Bands, designed Trophies & Plaques, appeared and spoke about the show on the local TV 
Sheet1
Cell Formulas
RangeFormula
B1:B20B1=BeforeComma(A1,Sheet2!A$1:A$147)
 
Upvote 0
this is what you want? (green table)
View attachment 4376
if so, use Power Query (Get&Transform)
Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ETBD = Table.TransformColumns(Source, {{"RAW", each Text.BeforeDelimiter(_, ",", {0, RelativePosition.FromEnd}), type text}}),
    ITBD = Table.AddColumn(ETBD, "Text Before Delimiter", each Text.BeforeDelimiter([RAW], " "&[keywords], {0, RelativePosition.FromEnd}), type text),
    ROC = Table.SelectColumns(ITBD,{"Text Before Delimiter"})
in
    ROC

Sandy, your power query looks good and does extractly what I need.

I do not know how to use power query.
Can you convert the code into vba?
 
Upvote 0
In that case, try this user-defined function.

VBA Code:
Function BeforeComma(s As String, rWords As Range) As String
  Static RX As Object
  Dim Bits As Variant
  Dim tmp As String
  Dim i As Long
 
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.IgnoreCase = True
  End If
  RX.Pattern = "^\b(" & Join(Application.Transpose(rWords), "|") & ")\b[^#]*\,"
  Bits = Split(s)
  For i = UBound(Bits) To 0 Step -1
    tmp = Bits(i) & " " & tmp
    If RX.test(tmp) Then
      BeforeComma = Trim(Left(s, Len(s) - Len(tmp)))
      Exit For
    End If
  Next i
End Function

Here is the sample data and results (using the original list in column A of Sheet2 of the sample file that you provided).

Book1
AB
1 Coordinated team in bringing about Business Requirements, Architectural Specifications, CorporateCoordinated team in bringing
2Major accomplishment: The data transfer from about 95 databases, are continuously loaded into one database in less than 15 minutes, after numerous aggregations and calculations.Major accomplishment: The data transfer from about 95 databases, are continuously loaded into one database in less
3Pre-screened and pre-counseled potential applicants; provided social and academic information about college life and experiences; guided campus tours; spoke on student panels; contacted, arranged, and visited high schools to represent and promote Colorado State University.Pre-screened and pre-counseled potential applicants; provided social and academic information about college life and experiences; guided campus tours; spoke
4 · Hobbies: Reading about grid computing, computer forensics and data analysis, Arts· Hobbies: Reading
5College Editor Revamped and elevated quality of alumnae magazine by giving marketing-focused publication a journalistic slant through the telling of vibrant stories about the students, alumnae, and faculty.College Editor Revamped and elevated quality of alumnae magazine by giving marketing-focused publication a journalistic slant through the telling of vibrant stories
6ï‚·Developed a smart rule engine for CAS that tracks, alerts, emails and generates Crystal/Jasper reports for decision-makers in a company about any customer-employee issue whenever the rule is applicable. 
7Administrator Module to create, edits, delete and enquire about the registeredAdministrator Module
8 about 3,000 bank branches in some two dozen western and mid western states, in addition toabout 3,000 bank branches
9 information about the visitor's behavior, tracking campaign effectively and see how visitors found the site, howinformation
10 and brand identity; marketing, cross-selling of products, as well as building customer awareness about the organization and its services.and brand identity; marketing, cross-selling
11 Regular Interaction with the client team appraising them about project status, risks, challenges & resourceRegular Interaction with the client team appraising them
12 Regular Interaction with the client team appraising them about project status, risks, challenges & resourceRegular Interaction with the client team appraising them
13Knowledge about different process such as Negative news check, transaction settlement ,Knowledge about different process such
14 5% of revenue to about 30% in 4 years, improved service quality, shortened time to market for new offerings5% of revenue to about 30%
15 Gained knowledge about networking protocols (IPv6, TCP), IP Networking Infrastructure and itsGained knowledge
16 Gained knowledge about the North American digital hierarchy including DS0, DS1, DS3 andGained knowledge about the North American digital hierarchy
17 social network in planning, budgeting, shopping and communicating about her weddingsocial network
18 for a beef plant with an annual revenue of about $600,000,000.for a beef plant with an annual revenue of
19 of educating them about technologies to increase business productivity, save time, and meetof educating them about technologies to increase business productivity,
20 traffic flow for the Bands, designed Trophies & Plaques, appeared and spoke about the show on the local TV 
Sheet1
Cell Formulas
RangeFormula
B1:B20B1=BeforeComma(A1,Sheet2!A$1:A$147)

Your VBA does not do correct extraction.
It have to extract like Sandy's Power query.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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