+ Reply to Thread
Results 1 to 5 of 5

Delete from a cell all words shorter than N characters

  1. #1
    Registered User
    Join Date
    02-04-2012
    Location
    CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    56

    Delete from a cell all words shorter than N characters

    Hi all,

    I need to delete from a cell all the words shorter than N characters, leaving only the words equal or longer than N characters.

    Example
    Given: Cell contains "The brown bear ran quickly up the hillside"
    Desired result if N=3: "brown bear quickly hillside"

    Words are always separated with a single space.
    I'd prefer a formula. A leading and trailing space can be added if that will simplify things much.


    Thanks in advance for any help!

  2. #2
    Administrator JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Delete from a cell all words shorter than N characters

    I can't really fathom a straight formula to do this, though doing it with VBA would be quite simple. Let me know if a macro solution is acceptable.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-04-2012
    Location
    CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Delete from a cell all words shorter than N characters

    JB - Yes, I'll accept a macro approach instead of a formula.

  4. #4
    Administrator JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Delete from a cell all words shorter than N characters

    This is a User Defined Function you can install that will give you a new function you can use in a cell.
    Please Login or Register  to view this content.
    How to install the User Defined Function:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save your workbook

    The function is installed and ready to use.

    If your original sentence is in cell A1, then in B1 you can use this new formula:

    =KeepN(A1, 4)

    The first parameter is the sentence, the second parameter is the number of character required for a word to be kept.


    If you don't want to use the formula in an adjacent cell, but you would rather have the original cell(s) changed, then install this macro, too, in the same module, paste it in below the one given above:

    Please Login or Register  to view this content.
    With this macro, you would:

    1) Highlight all the cells you want to process
    2) Press Alt-F8 and choose the macro EliminateWords from the macro list to run

    The macro will ask you how many letters are required to keep a word, the default is 4.

    The macro will use the function given earlier to do the same thing but in the original highlighted cells.

  5. #5
    Registered User
    Join Date
    02-04-2012
    Location
    CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Delete from a cell all words shorter than N characters

    Perfect! Exactly what I needed. THANK YOU.

+ 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