+ Reply to Thread
Results 1 to 17 of 17

How to use vba to remove extra spaces between words

  1. #1
    Registered User
    Join Date
    07-30-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    32

    How to use vba to remove extra spaces between words

    Hi all,

    I am trying to remove extra spaces between words that are being inputed by a userform. I have tried uisng "= Trim(TextBox1.Text)" which works nicely for leading spaces and end spaces but is not effective for extra spaces between words. Anybody have an idea of what vba code is needed to remove the extra spaces between words??

    Thanks!

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    possibly not the most efficient but one method:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-30-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: How to use vba to remove extra spaces between words

    Is there any way to apply this code to an entire section on a worksheet??

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How to use vba to remove extra spaces between words

    This will remove upto 4 extra spaces in Column A:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    Note: the worksheet function version of trim does remove spaces between words, so you could also do something like:
    Please Login or Register  to view this content.
    which would probably run faster for big ranges.

  7. #7
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: How to use vba to remove extra spaces between words

    as an option
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    07-30-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: How to use vba to remove extra spaces between words

    Thanks for all the Help!

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    The worksheet function trim removes leading, following and middle spaces (converts more than one space to a single space between words):

    Please Login or Register  to view this content.
    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    @abousetta, I think in VBA it only removes leading and following spaces...

    I think to utilise the worksheet function you would need to do something like in post 6?

  11. #11
    Registered User
    Join Date
    08-20-2012
    Location
    Londonish, England
    MS-Off Ver
    Excel 2010
    Posts
    58

    Re: How to use vba to remove extra spaces between words

    The way I do it the Non VBA way is to just Find and replace " " with "". In VBA it would be something like:

    Please Login or Register  to view this content.
    Last edited by Chriz; 08-09-2013 at 04:55 AM. Reason: forgot code tags whoops!

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to use vba to remove extra spaces between words

    @ yudlugar

    abousetta is correct.

    VBa Trim() only removes leading and trailing spaces
    The Excel function TRIM() removes all extra spaces

    In VBa call the Excel function like so
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    Thanks Marcol, I assumed the worksheetfunction wouldn't be available as there was a vba version.

  14. #14
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    Thank Marcol, you beat me to the explanation.

    abousetta

  15. #15
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: How to use vba to remove extra spaces between words

    I have a macro with this in it as i get a lot of exported data with eccess spaces.
    basically it will turn all double spaces into single spaces and loop untill no double spaces remain
    Please Login or Register  to view this content.

  16. #16
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to use vba to remove extra spaces between words

    @ Leon V (AW
    ... basically it will turn all double spaces into single spaces and loop untill no double spaces remain
    Why?
    When Worksheetfunction.Trim() does the job without looping, see post #9

    Your code will not work properly if there are multiple leading or trailing spaces
    Last edited by Marcol; 08-09-2013 at 09:39 AM.

  17. #17
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: How to use vba to remove extra spaces between words

    @Leon V (AW)

    This one line should do the same thing as your described code as long as there are no formulas (e.g., all text):

    Please Login or Register  to view this content.
    abousetta

  18. #18
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: How to use vba to remove extra spaces between words

    Quote Originally Posted by Marcol View Post
    @ Leon V (AW

    Why?
    When Worksheetfunction.Trim() does the job without looping, see post #9

    Your code will not work properly if there are multiple leading or trailing spaces
    I use vba trim for leading and trailing. Thought Excels trim doesn't work on some Unix spaces like ASCII 160

+ 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 - Looking to remove period and extra spaces from column B
    By Xaos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2013, 01:15 PM
  2. Remove all spaces between words
    By Melf72 in forum Excel General
    Replies: 6
    Last Post: 09-06-2010, 08:11 PM
  3. Formula to remove spaces between words
    By jasonmcbride in forum Excel General
    Replies: 2
    Last Post: 03-31-2009, 08:54 PM
  4. Remove extra spaces
    By kanezfan in forum Excel General
    Replies: 1
    Last Post: 07-05-2007, 02:06 PM
  5. [SOLVED] Remove spaces between words
    By Heather Tavitian in forum Excel General
    Replies: 2
    Last Post: 09-30-2005, 09:05 PM

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