+ Reply to Thread
Results 1 to 7 of 7

Thread: Trim Spaces(all)

  1. #1
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Trim Spaces(all)

    I have been trying to write some code that will trim all spaces in between words of different length with no success, I can trim the spaces before and after, but no the spaces between words. Any one know some code to do this?
    Last edited by JapanDave; 02-09-2012 at 09:04 PM.
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  2. #2
    Valued Forum Contributor
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    Excel 2010
    Posts
    522

    Re: Trim Spaces(all)

    Maybe something like this (I took an example from another thread)
    Sub ert()
    With Range("E1:E" & Cells(Rows.Count, 5).End(xlUp).Row)
         .Offset(, 3).Value = Application.Trim(.Value)
    End With
    End Sub
    Attached Files Attached Files

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7/winXP
    Posts
    2,180

    Re: Trim Spaces(all)

    Option Explicit
    
    Sub MiddleSpaces()
        Dim Fragments   As Variant, _
            Ctrl        As Long, _
            NewString   As String
        
        Fragments = Split(Range("A1").Value, " ")
        
        For Ctrl = LBound(Fragments) To UBound(Fragments)
            NewString = NewString & IIf(Fragments(Ctrl) <> "", Fragments(Ctrl) & " ", "")
        Next Ctrl
        
        NewString = Trim(NewString)
    
    End Sub
    ---
    Ben Van Johnson

  4. #4
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Trim Spaces(all)

    Hi

    Another version
    Sub aaa()
      Dim regex As Object
      Set regex = CreateObject("vbscript.regexp")
      
      With regex
        .Pattern = "\s{2,}"
        .Global = True
      End With
      
      For Each ce In Range("A2:A4")
        ce.Value = Trim(regex.Replace(ce, " "))
      Next ce
      
    End Sub
    rylo

  5. #5
    Valued Forum Contributor
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    Excel 2010
    Posts
    522

    Re: Trim Spaces(all)

    or
    Sub rty()
    Dim i As Byte
    For i = 1 To 3 ' 3 or more
        Range("E1:E" & Cells(Rows.Count, 5).End(xlUp).Row).Replace "  ", " "
    Next i
    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Trim Spaces(all)

    @ nilem,

    Thank you nilem, I shall use your Code as per Post#2

  7. #7
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Trim Spaces(all)

    Thanks guys, I have now got it sorted!
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

+ 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.2.0