+ Reply to Thread
Results 1 to 10 of 10

Eliminating spaces within text cells

  1. #1
    Registered User
    Join Date
    12-09-2009
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    61

    Eliminating spaces within text cells

    I have a formula that is dependent upon a column of cells containing text. Cells within this column randomly have an additional space (" ") following the words. With this invisible space, the formula doesn't work as intended.

    Since I have an extremely long list of names, is there a way to easily remove additional spaces after words without manually going through each cell and deleting them?

  2. #2
    Forum Contributor
    Join Date
    02-14-2007
    Location
    Chester, England
    MS-Off Ver
    2010 & 2016
    Posts
    312

    Re: Eliminating spaces within text cells

    Hello

    If you just want to just get rid of the spaces at the end and begining and where there are multiple spaces together then you could use.......

    =TRIM(A2)

    If you need to get rid of all spaces in a range, the highlight the range, press ctrl + H and the put a space in the top box and leave the bottom box then press "replace all"

    Hope this helps

    Jim

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

    Re: Eliminating spaces within text cells

    Try this macro:
    Please Login or Register  to view this content.
    How/Where to install the macro:

    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 sheet

    The macro is installed and ready to use.


    1) Select the cells that need to be fixed
    2) Press Alt-F8 and select TrimSelection from the macro list
    _________________
    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!)

  4. #4
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Eliminating spaces within text cells

    hi all,
    Here's a slight twist on JB's version which may run slightly faster (depending on the size of the list).

    Please Login or Register  to view this content.
    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

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

    Re: Eliminating spaces within text cells

    Heh....yours is WAY faster on a large dataset, as expected. Nice.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Eliminating spaces within text cells

    Here's another way. Make sure the selection does not contain any formulas.
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Eliminating spaces within text cells

    data /text to columns /fixed width/ finish
    strips off leading and trailing spaces
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  8. #8
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Eliminating spaces within text cells

    hi all,

    dforte,
    If your problem is solved, can you please mark the thread as solved?

    Thanks JB
    I can't remember clearly where I first saw this technique, but I think it was one of Walkenbach's Power Programming books. Here's a link showing an example with some discussion: http://www.dailydoseofexcel.com/arch...nge-using-vba/

    Shg & Martin, nice suggestions :-)

    Shg, can you please talk me through how your one works?
    Please Login or Register  to view this content.
    I can make the concept work as a CSE entered array formulae in a range of cells in Excel but I don't understand how the logical_test meets the Help files statement of being "Any value or expression that can be evaluated to TRUE or FALSE."

    Rob

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Eliminating spaces within text cells

    Please Login or Register  to view this content.
    The Evaluate function will return an array of values if it's persuaded. It get persuaded by the ROW(range) function, which returns an array of numbers that are the row numbers of the cells being evaluated. The numbers are coerced to Boolean (all True, since they are all non-zero), which causes the Trim function to be evaluated for each.

  10. #10
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Eliminating spaces within text cells

    Quote Originally Posted by shg View Post
    ...
    The numbers are coerced to Boolean (all True, since they are all non-zero)...
    Thanks Shg, that's the final piece of the puzzle, which helps me understand the "why" rather than just knowing that "it works".

    Thanks
    Rob

+ 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