+ Reply to Thread
Results 1 to 18 of 18

How to use vba to remove extra spaces between words

Hybrid View

  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:
    Sub macro_1()
    Dim str, str1, i
    str = " asdf   asdfo asdfon as    asdfon  as"
    str = Split(str)
    For i = 0 To UBound(str)
        If Not str(i) = "" Then str1 = str1 & " " & str(i)
    Next
    str = Trim(str1)
    End Sub

  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

    Note: the worksheet function version of trim does remove spaces between words, so you could also do something like:
    Sub macro_2()
    Dim ws1, ws2
    Set ws1 = ActiveSheet
    Set ws2 = Sheets.Add
    ws2.Range("A1:Z100").FormulaR1C1 = "=trim(" & ws1.Name & "!rc)"
    ws2.Range("A1:Z100").Copy
    ws1.Range("A1").PasteSpecial xlValues
    Application.DisplayAlerts = False
    ws2.Delete
    Application.DisplayAlerts = True
    End Sub
    which would probably run faster for big ranges.

  5. #5
    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

    Sub macro_1()
    Dim rng, str, str1, i, cl
    Set rng = Range("A1:Z100") 'change to desired range
    For Each cl In Range("A1:Z100").Cells
        If Not cl.Text = "" Then
            str = cl
            str = Split(str)
            For i = 0 To UBound(str)
                If Not str(i) = "" Then str1 = str1 & " " & str(i)
            Next
            cl = Trim(str1)
        End If
    Next
    End Sub

  6. #6
    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:

    Sub JoeWalsh()
    With Range("A2:A" & ActiveSheet.UsedRange.Rows.count)
        .Replace " ", " ", xlPart
        .Replace "  ", " ", xlPart
        .Replace "   ", " ", xlPart
        .Replace "    ", " ", xlPart
    End With
    End Sub

  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
    Sub tt()
    Dim i&
    For i = 4 To 2 Step -1
        Range("A1:A10").Replace Space(i), " "
    Next
    End Sub

  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):

    Sub test()
      Range("A1:A10").Value = Application.Trim(Range("A1:A10").Value)
    End Sub
    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:

    Sub Delete_spaces()
    
        Cells.Replace What:=" ", Replacement:="", LookAt:=xlPart, SearchOrder:= _
            xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    End Sub
    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
    Worksheetfunction.Trim(Range("A2")
    or
    Application.Trim(Range("A2"))

  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
    'clean up descriptions
        Do While True
            Set Description = Cells.Find(What:="  ", After:=Range("A1"), LookIn:=xlFormulas, LookAt _
                :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                False, SearchFormat:=False)
            If Description Is Nothing Then Exit Do
            Range("Description").Replace What:="  ", Replacement:=" ", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
        Loop

  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 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

  18. #18
    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):

    ActiveSheet.UsedRange = Application.Trim(ActiveSheet.UsedRange)
    abousetta

+ 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