Wrap Text to Cell Below, not within same cell

Fay Girls

New Member
Joined
Nov 21, 2011
Messages
4
Hello!

I have this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strTarget As String, lngLen As Long, intRep As Integer, i As Integer
Const intSplit As Integer = 63
If Intersect(Target, Range("A:A")) Is Nothing Or Target.Count > 1 Then Exit Sub
If Len(Target.Value) > intSplit Then
strTarget = Target.Value
lngLen = Len(strTarget)
intRep = Int(lngLen / intSplit) + 1
For i = 1 To intRep
Target.Offset(i - 1, 0).Value = Mid(strTarget, (i - 1) * intSplit + 1, intSplit)
Next i

End If
End Sub

The way it is coded now, the text will wrap to the cell below (which is what I want), but it splits words when it hits 63 characters. Does anyone know how to change it so it wraps full words (<=63 characters)???? Please help!

Thank you!

Autumn
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Autumn,

Try modifying your code as below....

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strTarget As String, lngLen As Long, intRep As Integer, i As Integer, intSplit As Integer, myChar As String
intSplit = 63  '*** no longer constant
If Intersect(Target, Range("A:A")) Is Nothing Or Target.Count > 1 Then Exit Sub
If Len(Target.Value) > intSplit Then
 
'Work back thro' string to find a space
Do Until myChar = " "
myChar = Mid$(Target.Value, intSplit, 1)
intSplit = intSplit - 1
Loop
 
strTarget = Target.Value
lngLen = Len(strTarget)
intRep = Int(lngLen / intSplit) + 1
For i = 1 To intRep
Target.Offset(i - 1, 0).Value = Mid(strTarget, (i - 1) * intSplit + 1, intSplit)
Next i
End If
End Sub

Let me know if that helps.

Tony
 
Upvote 0
Hello! Thanks for trying to help. The text isn't wrapping to the cell below. Any other suggestions? The code I posted initially wrapped the text to the cell below after 63 characters, but it would split a word in half. Thanks for your help!

Autumn
 
Upvote 0
Autumn,

Unless I have misunderstood, this code appears to do the job ok at my end.

Just to confirm....

You are entering a string into a cell in column A.
Currently, if that string exceeds 63 characters then, when you hit enter, the characters in excess of 63 appear in the cell below. However, the fixed value of 63 means that sometimes words are split, part in entry cell and part below.

The only thing that I have changed in the original code that you posted is to adjust the value of variable intSplit. Instead of being the constant 63 as was it is adjusted to the point in the input string, of the first space to the left of the 63rd character. Your original code then splits and wraps the string at that point.

If there are still issues then can you post back with an exact copy of your new code.
Tony
 
Upvote 0
Works fine for me Tony.
The only thing I might mention to Autumn, is that the code will split the text at the nearest point before 63 characters.....So, if the last full word ends at 59 characters, that will be where it wraps.
 
Upvote 0
Oh my gosh! It worked. Initially, I put it in the existing excel doc that I had the other code in. When I put in the above code, it just didn't work. After you both confirmed to me that it worked for you....I tried it in a new file and waaalah! It worked! Thank you, thank you, thank you!!!

Autumn :)
 
Upvote 0

Forum statistics

Threads
1,215,753
Messages
6,126,674
Members
449,327
Latest member
John4520

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