Insert alt enter into a cell or a column with defined comma counted

Kevincwk2000

Board Regular
Joined
Mar 23, 2008
Messages
82
Dear Sir,

I am looking for a code to insert alt-enter into a cell or a column with a defined comma count per line, saying 5

is

C1,C2,C3,C4,C5,C6,C7
C8,C9,C10,C11

want to change the cell(s) as

C1,C2,C3,C4,C5,
C6,C7,C8,C9,C10,
C11

thanks,
Kevin
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi Kevin, Select you cell, Run the code.
Data Limited per line to 5 values.
Code:
Dim oCt, oNum As Integer, nNum As String
oCt = Split(Selection.Value, ",")

For oNum = 0 To UBound(oCt)
        If oNum <> 0 And (oNum + 1) Mod 5 = 0 Then
                nNum = nNum & oCt(oNum) & "," & Chr(10)
            Else
                nNum = nNum & oCt(oNum) & ","
        End If
Next oNum

With Range(Selection.Address)
.Value = nNum
.WrapText = True
End With
Mick
 
Upvote 0
Hi Mick,

run the code the cell will become,

C1,C2,C3,C4,C5,
C6,C7,
C8,C9,C10,
C11,

it is not my expected result.

thanks,
Kevin
 
Upvote 0
=AddLF(G10,",",5)
Code:
Function AddLF(txt As String, delim As String, n As Long) As String
Dim x, i As Long
x = Split(txt, delim)
For i = 0 To UBound(x) Step n
    If i <> 0 Then x(i) = vbLf & x(i)
Next
AddLF = Join(x, delim)
End Function
 
Upvote 0
Hi Jindon,

then the cell become

C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11

seems the code removed alt-enter only but no modify the cell into 5 comma per line.

Kevin
 
Upvote 0
How about
Code:
Function AddLF(txt As String, delim As String, n As Long) As String
Dim x, i As Long
x = Split(Replace(txt, vbNewLine, ""), delim)
For i = 0 To UBound(x) Step n
    If i <> 0 Then x(i) = vbNewLine & x(i)
Next
AddLF = Join(x, delim)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,544
Messages
6,120,126
Members
448,947
Latest member
test111

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