How to return one and two words before and after a specific word

MrsQ

New Member
Joined
Apr 14, 2012
Messages
3
Hello,

I am in need of a forumula. I am trying to extract one word before a keyword, one word after a keyword, 2 words before a keyword, and 2 words after a keyword.

For example:

The keyword is blue:


birds are blue today - 1 word before=are; 1 word after=today; 2 words before=birds are; 2 words after=today

blue is my favorite color - 1 word before=(null); 1 word after=is; 2 words before=(null); 2 words after=is my

see blue colors in the sky - 1 word before=see; 1 word after=colors; 2 words before=see; 2 words after=colors in


Any help would be very much appreciated!!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
hi,

add this code to module in VBA

Code:
Function getwords(sentence As String, keyword As String, opt As String)
getwords = ""
wrd = 0
x = Split(sentence, " ")
For Each word In x
   If UCase(word) = UCase(keyword) Then
      Select Case opt
          Case "1B"
             If wrd = 0 Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd - 1)
                Exit Function
             End If
          Case "2B"
             If wrd < 2 Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd - 2)
                Exit Function
             End If
          Case "1A"
             If wrd + 1 > UBound(x) Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd + 1)
             End If
          Case "2A"
             If wrd + 2 > UBound(x) Then
                getwords = ""
             Else
                getwords = x(wrd + 2)
             End If
      End Select
   End If
   wrd = wrd + 1
Next
End Function
you then have a new function called getwords

Useage:

getwords(Sentence,required word,answer type)

Answer types:

"1B" - 1 Before
"2B" - 2 Before
"1A" - 1 After
"2A" - 2 After

Example below


Excel Workbook
GHIJK
7Keywordblue
8Sentence1 Before2 Before1 After2 After
9birds are blue todayarebirdstoday 
10blue is my favorite color  ismy
11see blue colors in the skysee colorsin
12i like the colour bluecolourthe  
Sheet7
 
Last edited:
Upvote 0
WOW! That is incredible!

One question:

Is it possible for the 2B and 2A to actually contain 2 words OR just 1 word if there is only one word before/after blue?

For example:
in row 9, 2B=birds are (instead of birds); 2A=today
in row 10, 2A=is my (instead of is)
in row 11, 2B=see (instead of null); 2A=colors in (instead of in)
in row 12, 2B=the colour (instead of the)


I can always do a formula to concatenate what's already there but I thought I'd chek.

Thank you again!!!!! You are amazing! :grin:
 
Upvote 0
Hi,

replace previous function with this one

Code:
Function getwords(sentence As String, keyword As String, opt As String)
getwords = ""
wrd = 0
x = Split(sentence, " ")
For Each word In x
   If UCase(word) = UCase(keyword) Then
      Select Case opt
          Case "1B"
             If wrd = 0 Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd - 1)
                Exit Function
             End If
          Case "2B"
             If wrd < 2 Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd - 2) & " " & x(wrd - 1)
                Exit Function
             End If
          Case "1A"
             If wrd + 1 > UBound(x) Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd + 1)
             End If
          Case "2A"
             If wrd + 2 > UBound(x) Then
                getwords = ""
             Else
                getwords = x(wrd + 1) & " " & x(wrd + 2)
             End If
      End Select
   End If
   wrd = wrd + 1
Next
End Function
 
Last edited:
Upvote 0
Hi - Sorry i missed something - the previous new version did add 2 words, but didnt add 1 word if only 1 word was available, this version fixes that so should work exactly as requested :)

replace function with this function - formlas remain the same

Code:
Function getwords(sentence As String, keyword As String, opt As String)
getwords = ""
wrd = 0
x = Split(sentence, " ")
For Each word In x
   If UCase(word) = UCase(keyword) Then
      Select Case opt
          Case "1B"
             If wrd = 0 Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd - 1)
                Exit Function
             End If
          Case "2B"
             If wrd < 2 Then
                If wrd = 1 Then
                   getwords = x(wrd - 1)
                End If
                Exit Function
             Else
                getwords = x(wrd - 2) & " " & x(wrd - 1)
                Exit Function
             End If
          Case "1A"
             If wrd + 1 > UBound(x) Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd + 1)
             End If
          Case "2A"
             If wrd + 2 > UBound(x) Then
                If wrd + 1 <= UBound(x) Then
                   getwords = x(wrd + 1)
                End If
             Else
                getwords = x(wrd + 1) & " " & x(wrd + 2)
             End If
      End Select
   End If
   wrd = wrd + 1
Next
End Function
 
Upvote 0
Hey, it doesn't work for me. Here's the sample data

[TABLE="class: grid, width: 800, align: left"]
<tbody>[TR]
[TD][TABLE="width: 629"]
<tbody>[TR]
[TD="class: xl66, width: 629"]Dr. Arun Kumar Arunosha Labs Pvt Ltd
203/4 Local Shopping Centre Gujranwala Town, Part I Delhi – 110 009[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]=GetWords(A1,$B$13,"2A")[/TD]
[/TR]
[TR]
[TD][TABLE="width: 629"]
<tbody>[TR]
[TD="class: xl66, width: 629"]Dr. Rajiv Anand
D/222, Defence Colony ,
Opp Lajpat nagar Metro Stn
New Delhi – 110 024[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]=GetWords(A2,$B$13,"2A")[/TD]
[/TR]
[TR]
[TD][TABLE="width: 629"]
<tbody>[TR]
[TD="class: xl66, width: 629"]Dr. Sitara Kahai
E-43, Panchsheel Park New Delhi – 110 017[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]=GetWords(A3,$B$13,"2A")[/TD]
[/TR]
</tbody>[/TABLE]

B13 has this text: "Dr. " (D R PERIOD SPACE)

And this is the code of the module:

Code:
Function getwords(sentence As String, keyword As String, opt As String)
getwords = ""
wrd = 0
x = Split(sentence, " ")
For Each word In x
   If UCase(word) = UCase(keyword) Then
      Select Case opt
          Case "1B"
             If wrd = 0 Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd - 1)
                Exit Function
             End If
          Case "2B"
             If wrd < 2 Then
                If wrd = 1 Then
                   getwords = x(wrd - 1)
                End If
                Exit Function
             Else
                getwords = x(wrd - 2) & " " & x(wrd - 1)
                Exit Function
             End If
          Case "1A"
             If wrd + 1 > UBound(x) Then
                getwords = ""
                Exit Function
             Else
                getwords = x(wrd + 1)
             End If
          Case "2A"
             If wrd + 2 > UBound(x) Then
                If wrd + 1 <= UBound(x) Then
                   getwords = x(wrd + 1)
                End If
             Else
                getwords = x(wrd + 1) & " " & x(wrd + 2)
             End If
      End Select
   End If
   wrd = wrd + 1
Next
End Function

The idea is to get names out of the data, which are two words after "Dr. ". What's wrong here?

EDIT: The result that I get is "#NAME?" and the error is "The formula contains unrecognized text"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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