splitting text in a cell

bj6264

Board Regular
Joined
May 23, 2010
Messages
50
Hi, I have a cell containing text that I need to export into another application. however this application only takes 40 characters and needs the cells to wrap to a new row. ie if my cell is 83 characters long, I need 40 characters on the first row, 40 on the second row & 3 in the third. Can another help?
Thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Do you mean something like this?
Formula in A2 is copied down.

Excel Workbook
A
1This has a lot of text all in one cell. The text needs to be split into a number of cells. Each cell should have a a maximum of 40 characters.
2This has a lot of text all in one cell.
3The text needs to be split into a number
4of cells. Each cell should have a a max
5imum of 40 characters.
6
Long Text
 
Upvote 0
Activate cell with text.
Code:
[COLOR="Blue"]Sub[/COLOR] SplitText()

    [COLOR="Blue"]Dim[/COLOR] iLen [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR], s [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR], i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR], iShift [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR], j [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    
    iShift = 1
    [COLOR="Blue"]With[/COLOR] ActiveCell
        [COLOR="Blue"]If[/COLOR] Len(.Value) > 40 [COLOR="Blue"]Then[/COLOR]
            iLen = Len(.Value)
            [COLOR="Blue"]For[/COLOR] i = 1 [COLOR="Blue"]To[/COLOR] iLen
                s = s & Mid(.Value, i, 1)
                [COLOR="Blue"]If[/COLOR] i [COLOR="Blue"]Mod[/COLOR] 40 = 0 [COLOR="Blue"]Then[/COLOR]
                    ActiveCell.Offset(iShift, 0) = s
                    s = ""
                    iShift = iShift + 1
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR]
            [COLOR="Blue"]If[/COLOR] iLen [COLOR="Blue"]Mod[/COLOR] 40 <> 0 [COLOR="Blue"]Then[/COLOR]
                j = Fix(iLen / 40)
                [COLOR="Blue"]If[/COLOR] j * 40 > iLen [COLOR="Blue"]Then[/COLOR]
                    .Offset(iShift, 0) = Mid(.Value, j * 40, iLen - j * 40)
                [COLOR="Blue"]Else[/COLOR]
                    .Offset(iShift, 0) = Right(.Value, iLen - 40 * j)
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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