Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Find an exact word in a cell with many words

I have a column that contains a sentence in each cell. I want to look for an
exact word in each sentence in the entire column. How can I do this in a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should not get
A1 back even though "he" is found within the words "she" and "the." I can't
find any discussions exactly like this anywhere.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,118
Default Find an exact word in a cell with many words

With
A1: (a sentence)
D1: (a word or phrase to find......eg he)

This formula returns 1 if the ref cell contains the word, otherwise 0
B1: =MAX(COUNTIF(A1,{"","","* ","* "}&$D$1&{""," *"," *",""," *"}))

This formula returns "Yes" if the ref cell contains the word, otherwise "No"
B1: =IF(MAX(COUNTIF(A1,{"","","* ","* "}&$D$1&{""," *"," *","","
*"})),"Yes","No")

Copy either formula down as far as you need.

Is that something you can work with?
Post back if you have more questions.
--------------------------

Regards,

Ron
Microsoft MVP (Excel)
(XL2003, Win XP)

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to look for
an
exact word in each sentence in the entire column. How can I do this in a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should not get
A1 back even though "he" is found within the words "she" and "the." I
can't
find any discussions exactly like this anywhere.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,118
Default Find an exact word in a cell with many words

Unfortunate break point for the text wrap:

Here is the second formula in segments:
B1: =IF(MAX(COUNTIF(A1,{"","","* ","* "}&$D$1&
{""," *"," *",""," *"})),"Yes","No")

--------------------------

Regards,

Ron
Microsoft MVP (Excel)
(XL2003, Win XP)


"Ron Coderre" wrote in message
...
With
A1: (a sentence)
D1: (a word or phrase to find......eg he)

This formula returns 1 if the ref cell contains the word, otherwise 0
B1: =MAX(COUNTIF(A1,{"","","* ","* "}&$D$1&{""," *"," *",""," *"}))

This formula returns "Yes" if the ref cell contains the word, otherwise
"No"
B1: =IF(MAX(COUNTIF(A1,{"","","* ","* "}&$D$1&{""," *"," *","","
*"})),"Yes","No")

Copy either formula down as far as you need.

Is that something you can work with?
Post back if you have more questions.
--------------------------

Regards,

Ron
Microsoft MVP (Excel)
(XL2003, Win XP)

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to look for
an
exact word in each sentence in the entire column. How can I do this in a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should not
get
A1 back even though "he" is found within the words "she" and "the." I
can't
find any discussions exactly like this anywhere.






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Find an exact word in a cell with many words

Sub findword()
For Each c In Range("i1:i3")
x = InStr(c, "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to look for
an
exact word in each sentence in the entire column. How can I do this in a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should not get
A1 back even though "he" is found within the words "she" and "the." I
can't
find any discussions exactly like this anywhere.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Find an exact word in a cell with many words

can "he" be replaced with a variable?

"Don Guillett" wrote:

Sub findword()
For Each c In Range("i1:i3")
x = InStr(c, "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to look for
an
exact word in each sentence in the entire column. How can I do this in a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should not get
A1 back even though "he" is found within the words "she" and "the." I
can't
find any discussions exactly like this anywhere.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Find an exact word in a cell with many words

Also, this seems to only work if the word is not the first or last word in
the cell, correct? Since it identifies the word being exact by looking for a
space before and after the word.

If that is not true I am misunderstanding the code.

This is close though.

-Adam

"Don Guillett" wrote:

Sub findword()
For Each c In Range("i1:i3")
x = InStr(c, "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to look for
an
exact word in each sentence in the entire column. How can I do this in a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should not get
A1 back even though "he" is found within the words "she" and "the." I
can't
find any discussions exactly like this anywhere.



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Find an exact word in a cell with many words

regexp ?
(regular expressions)

"Adam_needs_help" wrote in message
...
Don, it works weel for the line you listed, and it is a big step in the
right
directiong. However, it does not work for:

a man is he
he is a man

Any ideas?

-Adam

"Don Guillett" wrote:

Tested using these lines
if she is walking the dog
it is a he don
it is heavy

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message
...
Also, this seems to only work if the word is not the first or last word
in
the cell, correct? Since it identifies the word being exact by looking
for a
space before and after the word.

If that is not true I am misunderstanding the code.

This is close though.

-Adam

"Don Guillett" wrote:

Sub findword()
For Each c In Range("i1:i3")
x = InStr(c, "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to
look
for
an
exact word in each sentence in the entire column. How can I do this
in
a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should
not
get
A1 back even though "he" is found within the words "she" and "the."
I
can't
find any discussions exactly like this anywhere.








  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Find an exact word in a cell with many words

try it this way
Sub findword()
On Error Resume Next
For Each c In Range("i1:i5")
x = InStr(Trim(c), "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = "" Or _
Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in message
...
Don, it works weel for the line you listed, and it is a big step in the
right
directiong. However, it does not work for:

a man is he
he is a man

Any ideas?

-Adam

"Don Guillett" wrote:

Tested using these lines
if she is walking the dog
it is a he don
it is heavy

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message
...
Also, this seems to only work if the word is not the first or last word
in
the cell, correct? Since it identifies the word being exact by looking
for a
space before and after the word.

If that is not true I am misunderstanding the code.

This is close though.

-Adam

"Don Guillett" wrote:

Sub findword()
For Each c In Range("i1:i3")
x = InStr(c, "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to
look
for
an
exact word in each sentence in the entire column. How can I do this
in
a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should
not
get
A1 back even though "he" is found within the words "she" and "the."
I
can't
find any discussions exactly like this anywhere.





  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Find an exact word in a cell with many words

Great, that seems to do the trick.

I follow just about everything, but what does the code:

Or _

Do, does it run the opposite of what comes before the Or?

"Don Guillett" wrote:

try it this way
Sub findword()
On Error Resume Next
For Each c In Range("i1:i5")
x = InStr(Trim(c), "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = "" Or _
Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in message
...
Don, it works weel for the line you listed, and it is a big step in the
right
directiong. However, it does not work for:

a man is he
he is a man

Any ideas?

-Adam

"Don Guillett" wrote:

Tested using these lines
if she is walking the dog
it is a he don
it is heavy

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message
...
Also, this seems to only work if the word is not the first or last word
in
the cell, correct? Since it identifies the word being exact by looking
for a
space before and after the word.

If that is not true I am misunderstanding the code.

This is close though.

-Adam

"Don Guillett" wrote:

Sub findword()
For Each c In Range("i1:i3")
x = InStr(c, "he")
If Mid(c, x - 1, 1) = " " And Mid(c, x + 2, 1) = " " Then MsgBox c.Row
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Adam_needs_help" wrote in
message ...
I have a column that contains a sentence in each cell. I want to
look
for
an
exact word in each sentence in the entire column. How can I do this
in
a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should
not
get
A1 back even though "he" is found within the words "she" and "the."
I
can't
find any discussions exactly like this anywhere.






  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Find an exact word in a cell with many words

Thanks to everyone for helping, I was able to get my code working!

"Adam_needs_help" wrote:

I have a column that contains a sentence in each cell. I want to look for an
exact word in each sentence in the entire column. How can I do this in a
macro?

Example:

Two cells contain:
A1: She is walking the dog
A2: It is a he

I want to find only the cells containing "he" which means I should not get
A1 back even though "he" is found within the words "she" and "the." I can't
find any discussions exactly like this anywhere.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to copy the first word or two words from a cell containing a complete sentence to another cell jonny Excel Discussion (Misc queries) 7 May 19th 23 03:43 AM
Routine to find exact Row matches in Col1 Col2 Col3 but exact offsetting numbers in Col4 [email protected] Excel Discussion (Misc queries) 0 June 11th 08 11:30 PM
How to filter a column of text for exact words Suzanne Excel Worksheet Functions 9 December 21st 07 10:41 PM
Delete words in cell if that word is in red kaiser Excel Programming 3 October 17th 05 07:51 AM
Separate last word in cell with more than 2 words? Pat Mayton Excel Worksheet Functions 2 March 24th 05 03:37 PM


All times are GMT +1. The time now is 10:34 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"