+ Reply to Thread
Results 1 to 16 of 16

search for keywords within many words in a cell

  1. #1
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    search for keywords within many words in a cell

    Hi,

    i am trying to figure out the coding to search for different keywords, then copy and paste them into another sheet in different columns.

    eg.

    cell 1A :
    john
    banana
    $1.2

    cell 2A :
    kelvin
    durian
    $2.4

    cell 3A :
    kenny
    orange
    $1.5

    cell 1B:
    kenny
    durian
    $1.3

    cell 2B:
    john
    banana
    $1.2

    the list goes on....from 1A to 30D

    all these data are in sheet1. My plan is to sort these data into sheet2.
    those data with john goes to 1st column, those with kelvin goes to column 2, those with kenny goes to 3rd column...and so on

    i have tried using wildcard, but nothing happens.
    Please Login or Register  to view this content.
    does anyone has any idea how to solve my problem? thanks =D
    Last edited by dan2010; 11-22-2010 at 08:00 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: search for keywords within many words in a cell

    Untested, but does this work?
    Please Login or Register  to view this content.

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: search for keywords within many words in a cell

    Try this assuming you have set your range and variables a, b, c
    Please Login or Register  to view this content.

    Hope this helps
    Last edited by Marcol; 11-20-2010 at 05:57 AM. Reason: Errors in code fixed
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: search for keywords within many words in a cell

    Use Excel's Builtin facilities.

    Please Login or Register  to view this content.



  5. #5
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: search for keywords within many words in a cell

    StephenR : the code is not valid

    Marcol : i tried your coding but nothing happens.

    snb : it returns runtime error 1004. " The command could not be completed by using the range specified.Select a single cell within the range and try the command again."

  6. #6
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: search for keywords within many words in a cell

    Please Login or Register  to view this content.
    i have tried this code but it does not work too. it always return k = 0. I have tried to use "john","kelvin","kenny" and it works. So is there any replacement coding for "*john*","*kelvin*","*kenny*" ?

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: search for keywords within many words in a cell

    My mistake try this
    Please Login or Register  to view this content.

    If this still does nothing, or doesn't give you the result you need, post a sample workbook.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: search for keywords within many words in a cell

    Please Login or Register  to view this content.
    It's imperative that A1 serves as a 'Fieldname'.
    Cfr. the attachment
    Attached Files Attached Files
    Last edited by snb; 11-20-2010 at 07:16 AM.

  9. #9
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: search for keywords within many words in a cell

    do u mind explaining
    Quote Originally Posted by snb View Post
    Please Login or Register  to view this content.
    why is cell D1 = A1? when the data are copied to column H,J,L

    and

    Quote Originally Posted by snb View Post
    Please Login or Register  to view this content.
    there is nothing in cell D2.

    and

    Quote Originally Posted by snb View Post
    Please Login or Register  to view this content.
    i do not understand the entire line of the code :S

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: search for keywords within many words in a cell

    the selectioncriteria for advanced filter will be put into D1:D2

    .usedrange.Columns(1).AdvancedFilter xlFilterCopy, .Range("D1:D2"), .Cells(1, 10 + (2 * j))

    the selectioncriteria indicate which field has to be filtered
    so D1 has to contain the same fieldname as the column to be filtered.
    that column's fieldname is in A1
    ergo put the fieldname of column 1 (A1) in the fieldselectorcriterion D1
    .Cells(1, 4) = .Cells(1, 1)

    put in D2 the value that has to be filtered in column 1
    .Cells(2, 4) = Choose(j, "*John*", "*Kelvin*", "*Kenny*")

    If you want to know what advancedfilter is :
    I can't explain better than F1, a good Excel-VBA-book, an Internet Excelcourse or a classroom Excel-VBA course can.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: search for keywords within many words in a cell

    Hello dan2010,

    When asking for explanations of code, you should also state if the code worked or not. If not, you should include the line where the failure occurred, and the number of the error. If it does work and you have no further changes that need to be made then please marked your post solved and thank those who helped you.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  12. #12
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: search for keywords within many words in a cell

    snb:
    Please Login or Register  to view this content.
    i assume this code is only for data in column A. how can i change it to search from B2 to E32? 2nd question, does fieldname refer to "*john*","*kelvin*","*kenny*"?

    Thank you for your explanation.

    Leith Ross: the code provided by snb is working but i do not understand how to modify the code to suit my needs, that's why i have not marked the post as solved. rest assured that i will do it when i have clarified all my doubts. sorry for the trouble.

  13. #13
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: search for keywords within many words in a cell

    i have copied all the data into column "z" in sheet("hidden2") and set the fieldname and criteria in cell y1 and y2. the problem is that i have 129 cells of raw data in column z but only around 80 cells are copied to column a to i. do u have any idea what is wrong with my coding? and is it possible to remove the fieldname? thank you.
    Please Login or Register  to view this content.

  14. #14
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: search for keywords within many words in a cell

    Dan - I suggest you post a sample workbook and I'm sure we can sort it out in no time.

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: search for keywords within many words in a cell

    If the range you want to inspect is B1:E32.

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: search for keywords within many words in a cell

    snb : i see. thank you. i will keep ur code for reference.

    StephenR : it is okay. i have found another "easier to understand" solution for my problem, using "if range.value like "*variable*" then..."

    i would like to thank StephenR, Marcol and especially snb for all the time and effort spent giving me all the help and suggestions. thank you very much =D

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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