auto wrap text to cell below after 63 characters

keithmct

Board Regular
Joined
Mar 9, 2007
Messages
232
Office Version
  1. 2019
Platform
  1. Windows
greetings all,

I have an invoice type spreadsheet which gathers info from other sheets. I want to be able to type in the description section of the invoice for work completed and after 63 characters automatically drop down to the cell below. Is that possible?
I've tried :
- wrapping text in same cell - don't like that outcome
- merging the whole section into one cell and having text start at the top, but I still need lines underneath to be available for another macro.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this in the Worksheet module:
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

edit: just tweaked (to make it work properly I think!)
 
Last edited:
Upvote 0
didn't work. I get an overflow error.
More info:
I have a section of the sheet from A30:J30 merged into one cell and copied down until row 42. Should I change any of the code to suit? (I tried all sorts of combinations)
 
Upvote 0
sorry had a typing error but now get a runtime error 11 :division by zero

in line

intRep = Int(lngLen/intSplit) +1

have to go to work. thanks for your help so far.
 
Upvote 0
What do you mean, "typing error"? Have you changed the code?

And what did you enter into the cell to get the "division by zero" message?

Which version of Excel are you using?
 
Upvote 0
Yard, I'm home from work now and found another typing error from this morning (maybe I was still half asleep). What you've given me works perfectly. Thanks heaps.

Keith.

ps. any chance you could PM me and explain exactly what your code does. This way I can learn and not just depend on everybody else.
 
Upvote 0
I had to transpose your code from 1 PC to another and made 2 typing errors in the process.

I'm using Vista/Office2007, but my spreadsheet is working under 2003 compatability mode.

It's all good now.
 
Upvote 0
Yard, I'm home from work now and found another typing error from this morning (maybe I was still half asleep). What you've given me works perfectly. Thanks heaps.

Keith.

ps. any chance you could PM me and explain exactly what your code does. This way I can learn and not just depend on everybody else.

Good, glad it works. Commented version below, hope it helps:

Rich (BB 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
 
'defines the range into which a relevant value might be entered (I assumed column A), 
'and checks that the number of cells being changed is not >1. 
'Reason for this is that it can get complicated trying to
'cope with a number of changes in one go.
If Intersect(Target, Range("A:A")) Is Nothing Or Target.Count > 1 Then Exit Sub
 
'checks the length of the value which has just been changed. 
'If this is greater than our constant of 63 then...
If Len(Target.Value) > intSplit Then
    'assign the string to a variable (for neatness)
    strTarget = Target.Value
   'assign the length of the string to a variable
    lngLen = Len(strTarget)
   'work out the number of cells over which to split the value: 
   'essentially the length of the string divided by 63 and rounded up to the nearest 1
    intRep = Int(lngLen / intSplit) + 1
   'loop through the string that number of times
    For i = 1 To intRep
       'offset from the changed cell downwards, and populate that cell with
       'the string which is 63 characters long and whose starting character
        'is a multiple of 63, starting at 0
        Target.Offset(i - 1, 0).Value = Mid(strTarget, (i - 1) * intSplit + 1, intSplit)
     'loop to the next iteration 
    Next i
 
End If
End Sub

Hope that helps?
 
Upvote 0
after some more experimentation, I have issues with this solution. Sometimes a word will be cut up (without a hyphen of course) and pasted to next row. Sometimes the next row starts with a blank.
I would really like it to word wrap, but not in the same cell, to the cell beneath.
 
Upvote 0

Forum statistics

Threads
1,203,052
Messages
6,053,223
Members
444,648
Latest member
sinkuan85

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