VBA: How To Replace Commas With Newlines In Cells in range In Excel and vice versa

jiobi

New Member
Joined
Feb 21, 2021
Messages
15
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
VBA: How To Replace Commas With Newlines In Cells in range In Excel and vice versa.
Data in cells in the range may be missing and may contain empty rows or columns.

Input
How, To, Replace, Commas, With, Newlines, In, Cells

Output
How
To
Replace
Commas
With
Newlines
In
Cells
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
VBA: How To Replace Commas With Newlines In Cells in range In Excel and vice versa.
Data in cells in the range may be missing and may contain empty rows or columns.

Input
How, To, Replace, Commas, With, Newlines, In, Cells

Output
How
To
Replace
Commas
With
Newlines
In
Cells
Try using the Text to column
 
Upvote 0
Assuming every comma will be followed by a space character...
VBA Code:
Sub ToggleCommaToNewline()
  Dim Cell As Range
  For Each Cell In Selection
    If InStr(Cell.Value, ",") Then
      Cell.Value = Replace(Cell.Value, ", ", vbLf)
    Else
      Cell.Value = Replace(Cell.Value, vbLf, ", ")
    End If
  Next
End Sub
 
Upvote 0
Here is a better way than what I posted earlier (no loops)...
VBA Code:
Sub ToggleCommaToNewline()
  If Selection.Find(",", , xlValues, xlPart, , , , , False) Is Nothing Then
    Selection.Replace vbLf, ", ", xlPart, , , , False, False
  Else
    Selection.Replace ", ", vbLf, xlPart, , , , False, False
  End If
  Selection.Rows.AutoFit
End Sub
 
Upvote 0
The VBA works only on the first cell.
Also, I want to reverse the process in another VBA.
 
Upvote 0
For me, either macro works on all cells in the range (one or more cells) that you select.
 
Upvote 0
"that you select" ! got it.
Now worked for me.
How to do the reverse?
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

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