+ Reply to Thread
Results 1 to 19 of 19

Find an exact word within a text string

  1. #1
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Find an exact word within a text string

    Hello guys,

    I come to this forum in order to make a question related to excel and vba. The thing is that I have a column with approximatly 150 keywords (Column A), and another column with 500 sentences (Column B), what I would like to obtain in the column C, is the keyword from the column A which is within the sentece in the column B. I mean, checking for example de Keyword in A1, with all the sentences in column B, and if this word is included in a sentece, then writting that word in the column C. When I say "included", I want to say that the word must be excatly the same. Example:

    A B C

    green The green car green
    blue The greenth house -
    red The red trousers red
    yellow He has a yellower wallet -


    It would be really appreciate an answer as soon as possible

    Thanks a lot and regards!

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Find an exact word within a text string

    In C2 try this formula..
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    Try this in order to not locate yellow.

    =IF(ISERROR(FIND(" " & A2 & " ", " " & B2 & " ")),"",A2)

    use SEARCH instead of FIND if you are not worried about case-senstive issue
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    Thanks a lot for your quick answers!! but neither of them worked. I have attached a sample of what I have, and what I want to achieve. Maybe the only solution is to create a function which takes every keyword from the column A and compare it with every sentence of the column B, and then it gives the word found in the column C. As a warning!, I would like to obtain in the column C, exactly the same Keyword. For example in the column b can be included the word "windows" which is different from the word "window" which is a keyword, therefore it is not the same and it should not appear in the column C. (Capital and small letters must be taken into account, since the words can be presented in both formats and are the same)

  5. #5
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    Here it is the file...
    Attached Files Attached Files

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    yes you will need a UDF.

    Your example file present a very different problem to the one suggested by your first data set, which is one of the reason the proposed solutions do not yield the results you want.

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    Please Login or Register  to view this content.
    C2: =udfKEYWORDS(B2,$A$2:$A$11,CHAR(32))

    third argument is delimiter character, if required

  8. #8
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    Thanks a lot Andy Pope!!! It works perfectly!! but the problem comes when the text string, in which I want to see if the keyword is included, is too long and then it appears a message saying (Value!). The thing is, when I delete "chart (32)" from the formula, it works. Why is it happening that? and what is exactly the meaning of "chart" in the formula? Should I have to remove from it or keep it?.

    Thanks a lot again!! your help has been really useful!!!

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    CHAR(32) is the same as space.

    You would need to post example of the problem so we can inspect what is going on.

  10. #10
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    According to you, I understand if the sentence has more than 32 spaces, it is not going to be taking account no? or how does it work?

  11. #11
    Registered User
    Join Date
    10-11-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Find an exact word within a text string

    Hi friend
    Try this formula according to my knowledge after used and then tell your valuable feedback.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    No, CHAR(32) is the same as " "
    That argument is the delimiter character used when building the concatenated string of keywords.

    As I said, post example so we can see what information you are using.

  13. #13
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    Yes, it is true, I have attached an example. As you can see in this file, keywords like: baterry, drive or webcam are not taken into account. I don't know if it is a problem with the function, or it is about the word "chart" within the function; but the thing is those words don't appear in the column C as words which are included in the sentences of the column B, and appear in the column A as Keywords. What can it be happening in this case? could you have a look and propose the "perfect solution"?

    Thanks a lot for your help!
    Attached Files Attached Files

  14. #14
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    The problem was the routine used spaces to chop up the words, as per you 2 example sets. This data set contains commas.
    So you first need to replace the commas with spaces.

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    Great!!!! it works awesome!!! now I have an another challenge for you! If a part from the commas, I use "/" or "-" or "." to separate the different words aswell. How would I have to modify the function? Would I have to introduce three new loops?. Thanks a million!!!

  16. #16
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    Add another argument to the function to define usable separators.

    =udfKEYWORDS(B2,$A$2:$A$13," ,.-/",CHAR(32))

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    This is what I have tried in order to remove ".",",","/" and "-". The problem comes when I obtain the results in the column C, because the function gives me back all the words that I want, but some of them repeated. What can I do to optimise this function? is there a better solution?. Thanks a lot for your help!!





    Function udfKEYWORDS(LookIn As Variant, UseWords As Variant, Optional Delimiter As Variant = " ") As Variant

    Dim strTemp As String
    Dim vntWord As Variant
    Dim strWord As String
    Dim vntItem As Variant
    Dim vntLookin As Variant


    strTemp = ""
    vntLookin = Replace(LookIn, ",", " ")

    For Each vntItem In Split(vntLookin, " ")
    If Len(Trim(vntItem)) > 0 Then
    For Each vntWord In UseWords
    strWord = Trim(vntWord)
    If Len(strWord) > 0 Then
    If StrComp(strWord, Trim(vntItem), vbTextCompare) = 0 Then


    If InStr(1, strTemp, strWord, vbTextCompare) > 1 Then
    ' already used
    Else
    strTemp = strTemp & vntItem & Delimiter

    End If
    Exit For
    End If
    End If
    Next
    End If
    Next
    vntLookin = Replace(LookIn, "-", " ")
    For Each vntItem In Split(vntLookin, " ")
    If Len(Trim(vntItem)) > 0 Then
    For Each vntWord In UseWords
    strWord = Trim(vntWord)
    If Len(strWord) > 0 Then
    If StrComp(strWord, Trim(vntItem), vbTextCompare) = 0 Then


    If InStr(1, strTemp, strWord, vbTextCompare) > 1 Then
    ' already used
    Else
    strTemp = strTemp & vntItem & Delimiter


    End If
    Exit For
    End If
    End If
    Next
    End If
    Next
    vntLookin = Replace(LookIn, "/", " ")
    For Each vntItem In Split(vntLookin, " ")
    If Len(Trim(vntItem)) > 0 Then
    For Each vntWord In UseWords
    strWord = Trim(vntWord)
    If Len(strWord) > 0 Then
    If StrComp(strWord, Trim(vntItem), vbTextCompare) = 0 Then


    If InStr(1, strTemp, strWord, vbTextCompare) > 1 Then
    ' already used
    Else
    strTemp = strTemp & vntItem & Delimiter


    End If
    Exit For
    End If
    End If
    Next
    End If
    Next
    vntLookin = Replace(LookIn, ".", " ")
    For Each vntItem In Split(vntLookin, " ")
    If Len(Trim(vntItem)) > 0 Then
    For Each vntWord In UseWords
    strWord = Trim(vntWord)
    If Len(strWord) > 0 Then
    If StrComp(strWord, Trim(vntItem), vbTextCompare) = 0 Then


    If InStr(1, strTemp, strWord, vbTextCompare) > 1 Then
    ' already used
    Else
    strTemp = strTemp & vntItem & Delimiter


    End If
    Exit For
    End If
    End If
    Next
    End If
    Next
    If Len(strTemp) > 0 Then
    strTemp = Trim(Left(strTemp, Len(strTemp) - 1))
    Else
    strTemp = "-"
    End If

    udfKEYWORDS = strTemp

    End Function

  18. #18
    Registered User
    Join Date
    07-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Find an exact word within a text string

    Andy pope, I do not have enough words to describe you!!! unbelievable!!! it works perfect!!! you are the master!!! by the way, if I wanted to add another exception like ";", would I have to add it in this way "=udfKEYWORDS(B2,$A$2:$A$13," ,.-/;",CHAR(32))"? Thanks a million!!!

  19. #19
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Find an exact word within a text string

    Yes that's correct.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Find Text String or Key Word Between Date Ranges & Sum Values
    By mycon73 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-19-2013, 07:22 PM
  2. [SOLVED] Find Exact Match Text String Using Formula
    By sweetrevelation in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-12-2012, 10:53 AM
  3. [SOLVED] HOW TO....Find a word in a text string then displaying in another cell ?
    By Jedski in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-11-2012, 02:10 PM
  4. Replies: 0
    Last Post: 07-11-2012, 06:05 AM
  5. [SOLVED] Find Word in text string - reproduce it in another cell
    By pinkgold in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2012, 02:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1