Try
Assuming the string is col.A.
Sub test()
Dim r As Range, txt As String
With CreateObject("VBScript.RegExp")
.Global = True
For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
r = Application.Trim(Replace(r, vbLf, " "))
.Pattern = "(.{1,27})( |$)"
r = Application.Trim(Replace(r, vbLf, " "))
r.Value = .Replace(r.Value, "$1" & vbLf)
.Pattern = "\n+(?!.)"
r.Value = .Replace(r.Value, "")
Next
End With
End Sub
If you are on Mac OS then
Sub testNonRegX()
Dim r As Range, txt As String, n As Long, x(), temp As Long
For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
txt = Application.Trim(Replace(r, vbLf, " "))
n = 0
Do While Len(txt) > 27
n = n + 1
ReDim Preserve x(1 To n)
temp = InStrRev(txt, " ", 28)
x(n) = Trim$(Left$(txt, temp))
txt = Trim$(Mid$(txt, temp + 1))
Loop
If Len(txt) Then
n = n + 1: ReDim Preserve x(1 To n)
x(n) = txt
End If
r.Value = Join(x, vbLf)
Next
End Sub
Bookmarks