+ Reply to Thread
Results 1 to 5 of 5

Using SUBSTITUTE for multiple items

  1. #1
    Registered User
    Join Date
    11-14-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Using SUBSTITUTE for multiple items

    I have a list of addresses in column A. Most of the addresses are fine, but some of the addresses have too many spaces between words, e.g. there might be one cell in column a which has six spaces like:
    Please Login or Register  to view this content.
    while another might have three spaces like:
    Please Login or Register  to view this content.
    I want to get rid of any double/triple/quadruple/nth spaces that exist and change them to just a single space. I know that in this case, n will never be more than 10.

    I know that the following formula works in B1:
    Please Login or Register  to view this content.
    That changes any double spaces to single spaces. And I could do another iteration of the same formula in C1 which would be:
    Please Login or Register  to view this content.
    which would reduce all the original triple spaces to single spaces (they became double spaces in column B), etc., and I could do that another 8 times across the next columns and I would achieve what I want.

    But surely there is a way to do this with one formula.

    I tried doing this in B1:
    Please Login or Register  to view this content.
    which caters for up to 10 spaces but Excel reports "#VALUE!" when I try that. I don't know why.

    Is there another way to do it? And is there a reason why the OR function that I tried is not working?

    Thank you so much in advance!

  2. #2
    Forum Expert Moo the Dog's Avatar
    Join Date
    09-28-2012
    Location
    Wisconsin
    MS-Off Ver
    Office 365 (PC & Mac)
    Posts
    1,845

    Re: Using SUBSTITUTE for multiple items

    Nooooooo... use the TRIM function... if your address with varying spaces is in cell A1, use this: =TRIM(A1)

    - Moo

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: Using SUBSTITUTE for multiple items

    I know this may be a long way round, but maybe you could use find/replace, based on teh same principle you are using now?

    ctrl/h find " " 2 spaces
    replace " " 1 space

    repeat a few times and yoy should be ablwe to get rid of the excess spaces

    edit: duh, use Moo's suggestion, dont know where my mind was
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    11-14-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Using SUBSTITUTE for multiple items

    Thank you both for your answers.

    I didn't know that TRIM got rid of spaces anywhere in the text, I thought it only did it for leading and trailing spaces. I just tried it and it works. Argh, I feel kind of stupid now.

    Thanks so much for your help, both of you!

  5. #5
    Forum Expert Moo the Dog's Avatar
    Join Date
    09-28-2012
    Location
    Wisconsin
    MS-Off Ver
    Office 365 (PC & Mac)
    Posts
    1,845

    Re: Using SUBSTITUTE for multiple items

    You're welcome.

    - Moo

    ... and don't feel stupid! There are tons of awesome tricks to be learned in Excel. Just browsing the posts in this Forum has helped me immensely!
    Last edited by Moo the Dog; 11-18-2012 at 09:23 PM.

+ 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