+ Reply to Thread
Results 1 to 18 of 18

Can't remove excessive spaces within a string

  1. #1
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Can't remove excessive spaces within a string

    I have the following situation.
    After importing data from a csv file certain cells contain strings that look like the one below.

    Please Login or Register  to view this content.
    As you can see at 2 places in the string there are excessive spaces. Infact, typically I get a carriage return followed by excessive spaces after a superscipt or subscript letter. I wrote a script that can remove the carriage returns but I don't know how to write a script to remove the spaces. Note: The lenght and the number of the spaces in a string varies.

    If you know how to remove these spaces with a VBA script please share it with me.

    The underlying problem is with the CSV files. I don't have control over the creation of the CSV files. The only way I can solve the problem from my end is by removing the carriage returns and excessive spaces after obtaining the CSV files.
    Last edited by dschmitt; 06-26-2012 at 03:52 AM.

  2. #2
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Can't remove excessive spaces within a string

    Could you send excel file with sample?
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: Can't remove excessive spaces within a string

    You can substitute 2 spaces with 1 space until all spaces a single

  4. #4
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Can't remove excessive spaces within a string

    tom1977. No, I can't send an Excel file. Uploading files has been diabled at the company I work for. For testing purpose just copy paste the string into cell A1.
    zbor, that is an idea. How would that loop look like?

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Can't remove excessive spaces within a string

    Hi,

    Does the following non VBA (formula) solution help?

    =SUBSTITUTE(SUBSTITUTE(A1," ","|"),"|","")
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  6. #6
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Can't remove excessive spaces within a string

    Richard, that removes all spaces. Also I need a script because I need to incorporate this procedure into a larger macro.

  7. #7
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Can't remove excessive spaces within a string

    And what about TRIM function? (builtin or in VBA?)

  8. #8
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Can't remove excessive spaces within a string

    As far as I know the trim function only works for trimming right or left or left and right of a string. Not within a string. I may be wrong.

  9. #9
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: Can't remove excessive spaces within a string

    Richard idea is best...
    Just put space in second substitute:

    =SUBSTITUTE(SUBSTITUTE(A1," ","|"),"|"," ")

  10. #10
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Can't remove excessive spaces within a string

    zbor, that doesn't work. It does not remove the excessive spaces.

  11. #11
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Can't remove excessive spaces within a string

    Try function =trim(a1) and if the result is ok then it will be easy to transfer this in vba.

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,440

    Re: Can't remove excessive spaces within a string

    TRIM formula will remove spaces from left and right ends as well as multiple spaces within text.
    The help says.
    Removes all spaces from text except for single spaces between words. Use TRIM on text that you have received from another application that may have irregular spacing.
    VBA TRIM function only tops and tails a string. But you can use the worksheet function in VBA.
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  13. #13
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Can't remove excessive spaces within a string

    this works: =SUBSTITUTE(SUBSTITUTE(A1," ","|"),"|","")
    however, trim is better if it also removes spaces within the string. I will try that now.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Can't remove excessive spaces within a string

    try
    Please Login or Register  to view this content.

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,440

    Re: Can't remove excessive spaces within a string

    You will get different results between that formula and TRIM depending on the number of consecutive spaces in the original string.

    Test the following 3 simple texts, where the last part denotes the number of spaces following the word Test.

    Please Login or Register  to view this content.

  16. #16
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Can't remove excessive spaces within a string

    I took Andy's script line and incorpoareted it adjusted into my macro. It works.
    Problem solved.

    Thanks everybody for all the great suggestions.
    Last edited by dschmitt; 06-26-2012 at 04:29 AM.

  17. #17
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Can't remove excessive spaces within a string

    Hi,

    Sorry for the typo. As Zbor has pointed out there should have been an additional space in the second element of the second substitute. Just to repeat

    =SUBSTITUTE(SUBSTITUTE(A1," ","|"),"|","")

    will return

    Epidermal growth factor chronically upregulates Ca2+-dependent Cl- conductance and TMEM16A expression in intestinal epithelial cells.

    If you need to incorporate it in macro then just write the macro so that it builds the SUBSTITUTE formula for you.

  18. #18
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Can't remove excessive spaces within a string

    Thanks Richard. I will keep this as an alternative in mind.

+ 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