Consulting

Results 1 to 8 of 8

Thread: Solved: Trimming multiple blanks

  1. #1

    Solved: Trimming multiple blanks

    Hi!

    I notice below trim command does not remove trim multiple blanks within the text, only at beginning and end. The trim function in excel does this and I wonder if anyone can tell me how I get the same result using vba?

    [VBA]
    Public Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False

    If Not Application.Intersect(Target, Range("ALL_INPUT_DATA")) Is Nothing _
    And Target.Count = 1 Then

    Target = Trim(UCase(Target))
    End If
    Application.EnableEvents = True
    End Sub
    [/VBA]

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Something like this:
    [vba]Public Sub Worksheet_Change(ByVal Target As Range)

    Do
    Target.Replace What:=" ", Replacement:=" ", LookAt:=xlPart
    Loop Until Application.CountIf(Target, "* *") = 0
    end sub
    [/vba]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Hmm, I get an everlasting loop by trying below. All cells I want to be trimmed are located in Range("TRIM_THESE").

    [vba]
    Public Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False

    If (Not Intersect(Target, Range("TRIM_THESE")) Is Nothing) Then
    Do
    Target.Replace What:=" ", Replacement:=" ", LookAt:=xlPart
    Loop Until Application.CountIf(Target, "* *") = 0
    End If

    Application.EnableEvents = True

    End Sub
    [/vba]

    What's wrong?

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    just do this then in a standard code module:
    [VBA]Sub del_xtra_spaces()
    Do
    Range("TRIM_THESE").Replace What:=" ", Replacement:=" ", LookAt:=xlPart
    Loop Until Application.CountIf(Range("TRIM_THESE"), "* *") = 0
    end sub
    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,055
    Location
    Aren't you just replacing one blank for another blank? Try removing the space between the quotation marks in the Replacement :=" " section

    [vba]Sub del_xtra_spaces()
    Do Range("TRIM_THESE").Replace What:=" ", Replacement:="", LookAt:=xlPart
    Loop Until Application.CountIf(Range("TRIM_THESE"), "* *") = 0
    End Sub[/vba]
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I didn't test it to tell you the truth, however, if you do that Ted ALL the spaces will be removed i think?, i've not got excel here at the moment so can't test but will do in an hour or so.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    If you just want to call the worksheet version of Trim() ...

    [vba]
    Public Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    If Not Application.Intersect(Target, Range("ALL_INPUT_DATA")) Is Nothing _
    And Target.Count = 1 Then
    Target = Application.WorksheetFunction.Trim(UCase(Target))
    End If

    Application.EnableEvents = True

    End Sub
    [/vba]

    Not tested

    Paul

  8. #8
    Quote Originally Posted by Paul_Hossler
    If you just want to call the worksheet version of Trim() ...

    [vba]
    Public Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    If Not Application.Intersect(Target, Range("ALL_INPUT_DATA")) Is Nothing _
    And Target.Count = 1 Then
    Target = Application.WorksheetFunction.Trim(UCase(Target))
    End If

    Application.EnableEvents = True

    End Sub
    [/vba]

    Not tested

    Paul
    Tested and works!

    Thanks - I will go with this solution!

    Rejje

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •