+ Reply to Thread
Results 1 to 9 of 9

Why doesn't this work? - Replace function

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Question Why doesn't this work? - Replace function

    Recently I've been trying to remove all instances of double spaces from a string. i.e. if I have a string
    Please Login or Register  to view this content.
    , I want to return "Test Me"


    I tried using the Replace function but I was surprised by the result. (Code provided at bottom of post to demonstrate)


    QUESTIONS:
    1. Why doesn't Replace remove all double spaces?
    2. Since Replace doesn't do what I want it to, what can I use instead?


    DEMONSTRATION OF REPLACE NOT REMOVING ALL DOUBLE SPACES
    Please Login or Register  to view this content.
    Last edited by mc84excel; 07-01-2014 at 06:27 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Why doesn't this work? - Replace function

    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Why doesn't this work? - Replace function

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    Am I doing something wrong? When I use that code I get:
    Please Login or Register  to view this content.

    I am looking for:
    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this work? - Replace function

    Why not use Application.Trim?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Why doesn't this work? - Replace function

    Brilliant Norie! +1


    Strangely enough, Trim was one of the first things I tried and it didn't work. Is there an application version of Trim that is different to Trim?

    nvm. Realised that Application.Trim = WorksheetFunction.Trim. For those like me who didn't know, read Help pages for Trim and WorksheetFunction.Trim. (One removes trailing & leading spaces, the other removes all spaces from text except for single spaces between words).
    Last edited by mc84excel; 07-01-2014 at 06:39 PM. Reason: updated to answer own question re versions of Trim

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this work? - Replace function

    The worksheet function Trim will remove remove all spaces between words apart from single spaces.

    Application.Trim is shorthand for Application.WorksheetFunction.Trim.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Why doesn't this work? - Replace function


    This is what occurs when thinking first VBA (Trim) instead of Excel (Application) ‼

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

    Re: Why doesn't this work? - Replace function

    In answer to the why doesn't replace work.

    It because it replaces first occurance of double space and then moves forward through the text. It does not start again from the begining of the text.
    The issue can more easily be seen by using a character rather than space.

    Please Login or Register  to view this content.
    So you would need to loop whilst checking for existence of double spaces.
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Why doesn't this work? - Replace function

    @ Andy Pope: That explains it. Thank you +1

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 05-13-2013, 06:23 PM
  2. VBA function doesn't work properly
    By sumonrezadu in forum Excel General
    Replies: 4
    Last Post: 08-28-2009, 09:38 AM
  3. Excel bug: forcing autoformatting, find/replace doesn't work
    By QuantumPion in forum Excel General
    Replies: 2
    Last Post: 06-24-2008, 04:39 PM
  4. NZ function doesn't work!
    By salut in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2006, 03:50 PM
  5. [SOLVED] Find and Replace dialog box in VBA doesn't work suddenly
    By Leung in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-18-2005, 03:07 AM

Tags for this Thread

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