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
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
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
Hi
Another version
ryloSub 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
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
@ nilem,
Thank you nilem, I shall use your Code as per Post#2
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks