Replace long spaces with Alt+Enter(s)

pooratlas

Board Regular
Joined
Apr 17, 2007
Messages
166
I receive an xls with text cells containing various long series of space to create carraige returns.
Picked up this elegant code on this bb (forgot the link).
The function cleans out long spacebands from text.
I'd like help to add Alt+Enter chr to the code, so that....

Several items in this cell sbsbsbsb format A sbsbsbsb copy II sbsb design New

becomes....

Several items in this cell
format A
copy II
design New

Function TrimSpace(strInput As String) As String
' This procedure trims extra space from any part of
' a string.

Dim astrInput() As String
Dim astrText() As String
Dim strElement As String
Dim lngCount As Long
Dim lngIncr As Long

' Split passed-in string.
astrInput = Split(strInput)

' Resize second array to be same size.
ReDim astrText(UBound(astrInput))
'MsgBox UBound(astrInput) '=152 characters

' Initialize counter variable for second array.
lngIncr = LBound(astrInput)

' Loop through split array, looking for
' non-zero-length strings.
SpaceFlag = 0

For lngCount = LBound(astrInput) To UBound(astrInput) '0 to 152
strElement = astrInput(lngCount)

If Len(strElement) > 0 Then
' Store in second array.
astrText(lngIncr) = strElement
lngIncr = lngIncr + 1
SpaceFlag = 0
End If
'MsgBox strElement 'every word or every space character
Next

'Msgbox strElement '=3 number of groups of words

' Resize new array.
ReDim Preserve astrText(LBound(astrText) To lngIncr - 1)

' Join new array to return string.
TrimSpace = Join(astrText)
End Function
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi pooratlas

1 - The trim function in the worksheet does the same as the code you posted. If you need it in vba you can just use:

Code:
Function TrimSpace(strInput As String) As String
    TrimSpace = Application.WorksheetFunction.Trim(strInput)
End Function

2 - For the function you asked you forgot to say how long should be the sequence of spaces to be replaced with LF.
In this example I replace any sequence of 2 or more spaces with LF. Let's start from here.

Code:
Function SpacesLF(strInput As String) As String

With CreateObject("vbscript.regexp")
    .Pattern = " {2,}"
    .Global = True
    SpacesLF = .Replace(strInput, vbLf)
End With
End Function

If that's not what you want then post back and define rigorously what you want, something like "erase the spaces before and after the string, a sequence of 2 to 4 spaces is replaced with one space and a sequence of more that 4 spaces is replaced with ALT_ENTER".

Remark: Don't forget that if you write it in a cell you have to turn Wrap Text on.
Hope this helps
PGC
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,016
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top