Creating a new row from one line where a single cell has multiple values.

Bluepig70

New Member
Joined
Jun 27, 2016
Messages
2
Good morning all,

First time poster so go easy !

I have a sheet which contains data held in 3 columns. One of the columns holds data with line feeds separating the columns. I would like to know how I could turn something like this :-

CELL A | CELL B | CELL C
==================================================
Class A | History | Fred <Ctrl+J> Wilma <Ctrl+J> Barney <Ctrl+J>

Into

CELL A | CELL B | CELL C
=====================
Class A | History | Fred
Class A | History | Wilma
Calss A | History | Barney

I've seen multiple scripts before however none allow for the other values to be replicated to the new line ?

Any help would be greatfully received !
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Right click the sheet name, select "View Code" to bring up the VBA editor. Now right click the VBAProject for your sheet and select Insert->Module. Paste the following in to the editor:

Code:
Option Base 0
Public Sub SplitCells()

Dim lastRow As Long
Dim rowLoop As Long
Dim aValue As String
Dim bValue As String
Dim splitValues() As String
Dim splitLoop As Long

lastRow = Range("A" & Rows.Count).End(xlUp).Row

rowLoop = 0
Do While rowLoop < lastRow
    rowLoop = rowLoop + 1
    aValue = Cells(rowLoop, 1).Value
    bValue = Cells(rowLoop, 2).Value
    splitValues = Split(Cells(rowLoop, 3).Value, vbLf)
    If UBound(splitValues) > 0 Then
        Rows(CStr(rowLoop + 1) & ":" & CStr(rowLoop + UBound(splitValues))).Insert
        For splitLoop = 0 To UBound(splitValues)
            Cells(rowLoop + splitLoop, 1).Value = aValue
            Cells(rowLoop + splitLoop, 2).Value = bValue
            Cells(rowLoop + splitLoop, 3).Value = Replace(splitValues(splitLoop), vbCr, "")
        Next splitLoop
        rowLoop = rowLoop + UBound(splitValues)
        lastRow = lastRow + UBound(splitValues)
    End If
Loop

End Sub

Back to your Excel sheet and use Alt+F8 to bring up the list of macros. Select SplitCells and run it. Please save your sheet before running the code just in case.

WBD
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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