You might have figured out from the above image what I am trying to explain. It has two rows and each cell in the first column has values that contain carriage returns. I have created the carriage returns by typing a text followed by pressing the keys Alt+Enter. However, each value in the text has separate meaning. Therefore, it would be nice if I could separate the values to multiple columns that have meaningful headers.
🚀 Excel shortcuts that you should know.
👉 You may also like ... How to find and highlight duplicate values in Excel using a simple VBA procedure
Macro to Split Values to Multiple Columns
Before writing the Macro, we will first add a "Button" control on our worksheet. This is optional.
The click event will execute the splitting procedure. Press Alt+F8 to create a new Macro. You can name it whatever you like.
In the VBA section, open the "Project Explorer" (press Ctrl+r) and expand "Microsoft Excel Objects". Find Sheet1 and double clict to open it. Now add the below code.
Option Explicit Private Sub CommandButton1_Click() Call splitText End Sub Sub splitText() Dim arr() As String Dim iRows, iTotalRows iTotalRows = Selection.Rows.Count ' Selection will get all selected rows. For iRows = 1 To iTotalRows arr = VBA.Split(Selection.Cells(iRows, 1), vbLf) ' split the selected text with line feed (vbLf).