+ Reply to Thread
Results 1 to 13 of 13

loop function to remove spaces.

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    loop function to remove spaces.

    Hi,
    I have a code to remove spaces but as there are up to 4 spaces before text, I want to loop the code but not sure how to do it.

    I tried
    Sub spacer()

    For Each r In ActiveSheet.UsedRange
    Do While Left(r.Value, 1) = " "
    If Left(r.Value, 1) = " " Then r.Value = Right(r.Value, Len(r.Value) - 1)
    Next r
    Loop

    End Sub

    Thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,685

    Re: loop function to remove spaces.

    Probably a stupid question, but why don't you just use Trim?

    Regards, TMS
    Last edited by TMS; 07-08-2014 at 07:37 PM. Reason: typo
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,685

    Re: loop function to remove spaces.

    Oh, forgot ... welcome to the forum, and please use Code Tags.


    Regards, TMS

  4. #4
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Re: loop function to remove spaces.

    I tried that but when I went to copy the trimmed data across as values, it wouldn't paste as some of the cells had merged cells. Can you apply a trim to a column in situ?

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,685

    Re: loop function to remove spaces.

    Please post a sample workbook with some typical data.

    Regards, TMS

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

    Re: loop function to remove spaces.

    Like TMS mentioned, this should work.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: loop function to remove spaces.

    Like TMS and jolivanes suggested, the trim function should work here. Another efficient way is to loop through the constant text values directly and use the trim$ function for strings:
    Please Login or Register  to view this content.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,685

    Re: loop function to remove spaces.

    The thread appears to have been marked solved which implies that one or other of the solutions proved useful to you.

    It is considered good practice, and good manners, to thank those people who have helped you ... especially if you want to get help from them in the future

  9. #9
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Re: loop function to remove spaces.

    I thought this worked but when I applied it, it seems to get stuck and keeps on running. I have to crash out of it.

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

    Re: loop function to remove spaces.

    I thought this worked
    Which code is this?

  11. #11
    Registered User
    Join Date
    06-26-2014
    Location
    New Plymouth Taranaki
    MS-Off Ver
    2013
    Posts
    29

    Re: loop function to remove spaces.

    This one
    Sub spacer()
    Application.ScreenUpdating = False
    Dim r As Range
    For Each r In ActiveSheet.UsedRange
    r.Value = Trim(r.Value)
    Next r
    Application.ScreenUpdating = True
    End Sub

    Tusen Takk

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

    Re: loop function to remove spaces.

    How many rows and columns with data do you have?
    Where does the cursor end up when you push Ctrl+End?
    Can you get rid of the merged cells? Merged cells gives a lot of problems.
    Last edited by jolivanes; 07-09-2014 at 01:01 AM.

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

    Re: loop function to remove spaces.

    If your sheet has formatted cells well past what you think is the UsedRange, you could try this.

    Please Login or Register  to view this content.
    BTW, using UsedRange can be very deceiving.

+ 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. [SOLVED] Excel 2007 : Function to remove spaces in telephone numbers
    By benoj2005 in forum Excel General
    Replies: 6
    Last Post: 06-27-2012, 06:16 AM
  2. How to remove #'s 0-9 and spaces
    By elite-fusion in forum Excel General
    Replies: 19
    Last Post: 04-18-2011, 04:48 PM
  3. Need to remove spaces
    By vonneffdobermans in forum Excel General
    Replies: 5
    Last Post: 02-27-2009, 01:46 AM
  4. How To Remove Spaces?
    By Dno in forum Excel General
    Replies: 4
    Last Post: 12-21-2006, 06:53 PM
  5. Replies: 5
    Last Post: 09-12-2005, 04:05 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