Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Len(Target) / Target.Width > Ratio Then SplitText
End Sub
Option Explicit
Public Const Ratio = 0.23
Sub SplitText()
Dim MyText As String
Dim WrapLength As Long, StrLen As Long, j As Long
Dim SplitRatio As Double
Dim NextCell As Range
SplitRatio = InputBox("Enter ratio", "Cell width/Characters", Ratio)
Application.EnableEvents = False
If Application.MoveAfterReturnDirection = xlToRight Then
Set NextCell = ActiveCell
ActiveCell.Offset(0, -1).Select
Else
ActiveCell.Offset(-1).Select
End If
WrapLength = Int(ActiveCell.Width) * SplitRatio
Do
MyText = ActiveCell.Text
StrLen = Len(MyText)
If StrLen > WrapLength Then
For j = WrapLength To 0 Step -1
If j = 0 Then Exit For
If Mid(MyText, j, 1) = " " Then
ActiveCell.Formula = Left(MyText, j)
ActiveCell.Offset(1, 0).Formula = Right(MyText, StrLen - j)
Exit For
End If
Next
ActiveCell.Offset(1, 0).Select
End If
Loop Until Len(ActiveCell) <= WrapLength
ActiveCell.Offset(1, 0).Select
If Not NextCell Is Nothing Then NextCell.Select
Application.EnableEvents = True
End Sub
Sub Retry()
Dim Cel
Dim MyText As String
Application.EnableEvents = False
For Each Cel In Selection
MyText = MyText & Cel
Cel.ClearContents
Next
Selection.Cells(1).Select
Selection.Formula = MyText
If Application.MoveAfterReturnDirection = xlToRight Then
ActiveCell.Offset(0, 1).Select
Else
ActiveCell.Offset(1).Select
End If
SplitText
End Sub
|