Sub test()
Dim x&, y&, oldpos, a&
t = "aaaa bbbb cccc dddd ee fff ggg hhhhhh iiii jj kkkk lll mmmmm nnn oo ppp qqqqq rrrrr ssssss tttt uuuuu vv www x yy zzz"
x = 0: y = 0
Do While x < Len(t)
x = InStr(y + 1, t, " ", vbTextCompare)
If x = 0 Then Exit Do
y = y + x
a = x
If (y - oldpos) > 30 Then x = InStrRev(Mid(t, 1, y), " ")
oldpos = y = y - a + x
Mid$(t, x, 1) = "*"
Loop
MsgBox t & vbCrLf & "----------------------" & vbCrLf & Replace(t, "*", vbCrLf)
End Sub
Sub test2()
Dim oldpos&, i&, t$
t = "aaaa bbbb cccc dddd ee fff ggg hhhhhh iiii jj kkkk lll mmmmm nnn oo ppp qqqqq rrrrr ssssss tttt uuuuu vv www x yy zzz"
oldpos = 0
For i = 1 To Len(t)
i = InStr(oldpos + 1, t, " ", vbTextCompare) 'jump en avant
If i = 0 Then Exit For 'sortie après la dernière occurrence de " "
If i - oldpos > 30 Then i = InStrRev(Mid(t, 1, i), " ") 'jump en arriere
oldpos = i 'memo old position
Mid$(t, i, 1) = "*"
Next
MsgBox t & vbCrLf & "----------------------" & vbCrLf & Replace(t, "*", vbCrLf)
End Sub
Sub essai()
t = "aaaa bbbb cccc dddd ee fff ggg hhhhhh iiii jj kkkk lll mmmmm nnn oo ppp qqqqq rrrrr ssssss tttt uuuuu vv www x yy zzz"
While Len(t) >= 30
If Mid(t, 31, 1) = " " Then
res = res & Left(t, 30) & vbCrLf
t = Mid(t, 31)
Else
x = Mid(t, 1, 30)
y = InStrRev(x, " ")
res = res & Left(x, y) & vbCrLf
t = Trim(Mid(t, y))
End If
Wend
Range("A2") = res & t
End Sub
Sub testPat()
Dim T$, L&
T = [A1].Text
L = Int([A1].ColumnWidth)
T = WrappWithAjustEntireWord3(T, L)
'[A1]=t
tb = Split(T, vbCrLf)
For i = 0 To UBound(tb): tb(i) = tb(i) & "-->" & Len(tb(i)) & " char": Next: T = Join(tb, vbCrLf)
MsgBox T
End Sub
Function WrappWithAjustEntireWord3(ByVal T$, ByVal L&)
T = Replace(Replace([A1].Value, vbCrLf, " "), Chr(10), " ")
Do Until i >= Len(T)
i = i + L
If i > Len(T) Then Exit Do
If Mid(T, i, 1) = " " Then
Mid(T, i, 1) = "*"
Else
i = InStrRev(Mid(T, 1, i), " "): Mid(T, i, 1) = "*"
End If
Loop
WrappWithAjustEntireWord3 = Replace(T, "*", vbCrLf)
End Function
Function justeuntest(ByVal T$, ByVal L&)
Dim matchs, r
With CreateObject("VBScript.RegExp"):
.Global = True: .IgnoreCase = True:
.Pattern = "([A-z-0-9]{" & L + 1 & "," & L + 20 & "})": Set matchs = .Execute(T)
If matchs.Count > 0 Then justeuntest = "un mot est trop long dans la chaine": Exit Function
.Pattern = "(\D{1," & L & "})\s": Set matchs = .Execute(T)
If matchs.Count > 0 Then
For i = 0 To matchs.Count - 1: r = r & Trim(matchs(i)) & "-->" & Len(Trim(matchs(i))) & vbCrLf: Next
justeuntest = r
End If
End With
End Function
Sub test1()
Dim T$, L&
T = [A1].Text
L = Int([A1].ColumnWidth)
MsgBox "pour A1" & vbCrLf & justeuntest(T, L)
T = [A2].Text
MsgBox "pour A2" & vbCrLf & justeuntest(T, L)
End Sub