+ Reply to Thread
Results 1 to 17 of 17

keep only the first word in a cell

  1. #1
    Forum Contributor
    Join Date
    10-27-2010
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    107

    keep only the first word in a cell

    I want to keep only the first word in a cell and remove all the other words in the cell

    For e.g. if a cell contains the word “R160 2.15k 7 7 f”,
    I want to remove “2.15k 7 7 f” and only keep R160

    I want to do this for a entire column A.

    Some picture illustration

    1) how the sheet looks like initially,
    1.JPG

    2) after running the macro, the sheet should looks like,
    2.JPG


    I have attached a sample workbook
    Sheet 1 contains a sample data
    Sheet2 shows how the data looks like after running the macro
    Sheet 3 contains a button to insert the codes
    removeWords.xlsx
    Last edited by darkhorse4321; 12-15-2010 at 09:41 AM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: keep only the first word in a cell

    Hi,

    Try this code

    Please Login or Register  to view this content.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  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: keep only the first word in a cell

    Try this

    =TRIM(LEFT(A2,FIND(" ",A2)))

    Hope this helps
    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: keep only the first word in a cell

    or
    Please Login or Register  to view this content.



  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: keep only the first word in a cell

    No need for a macro:
    Select the data, press Ctrl+H, enter a space followed by an asterisk in the Find box, leave the Replace box blank, and press Replace All.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Forum Contributor
    Join Date
    10-27-2010
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    107

    Re: keep only the first word in a cell

    Thanks guys for the help, all the above codes worked
    romperstomper, thanks for providing an alternative to solve this problem.It works too

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

    Re: keep only the first word in a cell

    Sure, you won't need a macro with:

    Please Login or Register  to view this content.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: keep only the first word in a cell

    Surely that is a macro by any normal definition?

  9. #9
    Forum Contributor
    Join Date
    10-27-2010
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    107

    Re: keep only the first word in a cell

    Hi everyone,

    When I tried all the above codes and methods with my sample worksheet I attached in the beginning of my thread, it worked

    But now when I tried the same codes and methods with my actual data, none of the above codes seem to work.

    I have attached my actual data below.
    These are data that were extracted from an external file
    remove.xls

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: keep only the first word in a cell

    You have to get rid of the leading spaces first.

    In cell B1:
    =TRIM(A1)

    In cell C1 put Marcol's formula: =TRIM(LEFT(B1,FIND(" ",B1)))

    Pull formulas down to the end of your used range in column A.

    Someone will combine these for you or come up with a code to use.

  11. #11
    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: keep only the first word in a cell

    You will need to use this in B2

    =LEFT(TRIM(A2),FIND(" ",TRIM(A2)))

    Or using sweeps macro
    Please Login or Register  to view this content.

    I get the impression from your sample you might actually want to use Text to Columns

    You should use the Forum facilities to upload a file, not some external source.

    From Forum FAQs

    How do I attach a file to a post?

    To attach a file to your post, you need to be using the main 'New Post' or 'New Thread' page and not 'Quick Reply'. To use the main 'New Post' page, click the 'Post Reply' button in the relevant thread.

    On this page, below the message box, you will find a button labelled 'Manage Attachments'. Clicking this button will open a new window for uploading attachments. You can upload an attachment either from your computer or from another URL by using the appropriate box on this page. Alternatively you can click the Attachment Icon to open this page.

    To upload a file from your computer, click the 'Browse' button and locate the file. To upload a file from another URL, enter the full URL for the file in the second box on this page. Once you have completed one of the boxes, click 'Upload'.

    Once the upload is completed the file name will appear below the input boxes in this window. You can then close the window to return to the new post screen.
    Last edited by Marcol; 12-16-2010 at 03:44 AM.

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

    Re: keep only the first word in a cell

    Quote Originally Posted by romperstomper View Post
    Surely that is a macro by any normal definition?
    I couldn't agree more.....

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

    Re: keep only the first word in a cell

    This will suffice:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Last edited by snb; 12-16-2010 at 04:36 AM.

  14. #14
    Forum Contributor
    Join Date
    10-27-2010
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    107

    Re: keep only the first word in a cell

    Thanks jolivanes for trying to help.

    Thanks Marcol, but your codes did not work for the latest file i uploaded in my previous reply. It cleared the entire cell contents

    Thanks snb the codes worked. i used the following codes of yours

    [A2:A350] = [if(A1:A350<>"",left(trim(A2:A350),find(" ",trim(A2:A350))),"")]
    the code will stop removing the cell content once it reach cell A350. But is it possible for the codes to do until the end of column A that is filled with content? Because i have few other files to do the same task and they all have contents until different rows in column A.

  15. #15
    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: keep only the first word in a cell

    There is no reason why
    In B2
    =LEFT(TRIM(A2),FIND(" ",TRIM(A2)))

    should not work considering this does
    Please Login or Register  to view this content.
    This is basically the same solution applied directly using VBa instead of a helper column.

    My personal preference is not to destroy source data until all is thoroughly tested, always keep a copy of sheets when using VBa solutions, they can't be undone.

    The formula works for me and can also be applied through VBa with

    Please Login or Register  to view this content.
    This allows for variable list lengths.

    [EDIT]

    I only copied sweeps' code and added a line, I should have posted
    Please Login or Register  to view this content.
    Last edited by Marcol; 12-16-2010 at 10:10 AM.

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

    Re: keep only the first word in a cell

    Please Login or Register  to view this content.
    Last edited by snb; 12-18-2010 at 08:37 AM.

  17. #17
    Forum Contributor
    Join Date
    10-27-2010
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    107

    Re: keep only the first word in a cell

    Thanks Marcol for your help, your codes below the edit column worked.

    Thanks snb your codes also worked.

+ 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