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 test()
Dim Chaine$, ChaineR$, Nbcar%, NbCarMax%, i%
Chaine = "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"
Nbcar = 0: NbCarMax = 30 ' NbCarMax : Nombre de caractères max par ligne
t = Split(Chaine, " ")
For i = 0 To UBound(t)
Nbcar = Nbcar + Len(t(i)) + 1 ' +1 pour compter l'espace manquant dû au Split
If Nbcar > NbCarMax Then
t(i) = vbCrLf & t(i) ' Ajout du saut de ligne car NbCarMax dépassé
Nbcar = Len(t(i)) - 1 ' Ré init de Nbcar avec le dernier élement, -1 pour ne pas compter le vbCrLf
End If
ChaineR = ChaineR & t(i) & " " ' Concaténation de la chaine de retour
Next i
MsgBox ChaineR
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
x = Mid(t, 1, 30)
y = InStrRev(x, " ")
res = res & Left(x, y) & vbCrLf
t = Trim(Mid(t, y))
Wend
Range("A2") = res & t
End Sub
Sub test3()
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
k = 1
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 Int(i / (30 * k)) > 0 Then Mid$(t, i, 1) = "*": k = k + 1
oldpos = i 'jump en arriere
'memo old position
Next
MsgBox t & vbCrLf & "----------------------" & vbCrLf & Replace(t, "*", vbCrLf)
End Sub
Sub test2()
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), " ")
Mid$(t, x, 1) = "*" ' l' * est rajouté uniquement si >30
End If
oldpos = y = y - a + x
Loop
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 essai()
t = "aaaaaaa bbbbbbbb 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 & Trim(Left(t, 30)) & vbCrLf
t = Trim(Mid(t, 31))
Else
x = Mid(t, 1, 30)
y = InStrRev(x, " ")
res = res & Trim(Left(x, y)) & vbCrLf
t = Trim(Mid(t, y))
End If
Wend
MsgBox res & t
End Sub
Sub zebanx()
Dim T$, L&
T = [A1].Value
L = Int([A1].ColumnWidth)
T = WrappWithAjustEntireWord2(T, L)
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 WrappWithAjustEntireWord2(ByVal T$, ByVal L&)
Dim oldpos&, i&
oldpos = 0
k = 1
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 Int(i / (l* k)) > 0 Then Mid$(T, i, 1) = "*": k = k + 1
oldpos = i 'memo old position
Next
If Asc(Mid(T, Len(T))) = 10 Then T = Left(T, Len(T) - 1)
WrappWithAjustEntireWord2 = Replace(T, "*", vbCrLf)
End Function
Sub test()
Dim T$, L&
T = [A1].Value
L = Int([A1].ColumnWidth)
T = WrappWithAjustEntireWord(T, L)
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
'[A1]=t
End Sub
Function WrappWithAjustEntireWord(ByVal T$, ByVal L&)
Dim Res$, X$, Y&
While Len(T) >= L
If Mid(T, L + 1, 1) = " " Then
Res = Res & Trim(Left(T, L)) & vbCrLf
T = Trim(Mid(T, L + 1))
Else
X = Mid(T, 1, L)
Y = InStrRev(X, " ")
Res = Res & Trim(Left(X, Y)) & vbCrLf
T = Trim(Mid(T, Y))
End If
Wend
If Asc(Mid(T, Len(T))) = 10 Then T = Left(T, Len(T) - 1)
WrappWithAjustEntireWord = Res & T
End Function
sub pourTest()
[A1].columnwidth=20
[A1]="aaaaaaaaaaaaaaaaaa bbbbbbbb cccc dddd ee fff ggg hhhhhh iiii jj kkkk lll mmmmm nnn oo ppp qqqqq rrrrr ssssss tttt uuuuu vv www x yy zzz"
end sub
Salut Patricket ben !!!...encore une fois t'a pris ton temps toi hein!!!