Creating Duplicate Rows

tryingmybest418

New Member
Joined
Jan 22, 2018
Messages
32
Hi all,

I have a worksheet where each row represents a location. Column C lists all of the employees at that location separated by carriage returns.

Is it possible for the sheet to split out the employees and display individual lines for each employee?

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Location ID[/TD]
[TD]Location[/TD]
[TD]Employees[/TD]
[TD]Manager[/TD]
[TD]Budget[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Alex Anderson

Bob Barney

Chris Conners
[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]200[/TD]
[TD]Atlanta[/TD]
[TD]Dan Davidson

Emmit Erickson

Fran Fredrickson[/TD]
[TD]Ms. Supervisor[/TD]
[TD]300,000[/TD]
[/TR]
</tbody>[/TABLE]


[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Location ID[/TD]
[TD]Location[/TD]
[TD]Employee[/TD]
[TD]Manager[/TD]
[TD]Budget[/TD]
[/TR]
[TR]
[TD]100
[/TD]
[TD]Chicago[/TD]
[TD]Alex Anderson[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Bob Barney[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Chris Conners[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]200[/TD]
[TD]Atlanta[/TD]
[TD]Dan Davidson[/TD]
[TD]Ms. Supervisor[/TD]
[TD]300,000[/TD]
[/TR]
[TR]
[TD]200[/TD]
[TD]Atlanta[/TD]
[TD]Emmit Erickson[/TD]
[TD]Ms. Supervisor[/TD]
[TD]300,000[/TD]
[/TR]
[TR]
[TD]200[/TD]
[TD]Atlanta[/TD]
[TD]Fran Fredrickson[/TD]
[TD]Ms. Supervisor[/TD]
[TD]300,000[/TD]
[/TR]
</tbody>[/TABLE]

Thanks!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Code:
Sub Split_Cr()
Dim rng As Range, cel As Range, cr%
Set rng = Range([C2], Cells(Rows.Count, "C").End(xlUp))
For Each cel In rng
    cr = Len(cel) - Len(Replace(cel, vbLf, ""))
    If cr > 0 Then
        cel(2).Resize(cr).EntireRow.Insert
        cel.Resize(cr + 1) = WorksheetFunction.Transpose(Split(cel, vbLf))
    End If
Next
Set rng = Range([A2], Cells(Rows.Count, "C").End(xlUp)(1, 3))
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
rng = rng.Value
End Sub
 
Upvote 0
This is great, thank you.

I made a couple adjustments, and it's working better, but there's still an issue:

My worksheet isn't exactly like the example, I have columns through J, so I changed the Set rng line to (1, 8)

I also moved the entire rng portion inside the If statement.

It seems to be close, but i'm getting duplicates- see my new code and example below:

[TABLE="class: cms_table_grid, width: 500"]
<tbody>[TR]
[TD]Location ID[/TD]
[TD]Location[/TD]
[TD]Employees[/TD]
[TD]Manager[/TD]
[TD]Budget[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Alex Anderson

Bob Barney

Chris Conners[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]200[/TD]
[TD]Atlanta[/TD]
[TD]Dan Davidson

Emmit Erickson

Fran Fredrickson[/TD]
[TD]Ms. Supervisor[/TD]
[TD]300,000[/TD]
[/TR]
</tbody>[/TABLE]



[TABLE="class: cms_table_grid, width: 500"]
<tbody>[TR]
[TD]Location ID[/TD]
[TD]Location[/TD]
[TD]Employee[/TD]
[TD]Manager[/TD]
[TD]Budget[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Alex Anderson[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Alex Anderson[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Bob Barney
[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Bob Barney[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Chris Conners[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]Chicago[/TD]
[TD]Chris Conners[/TD]
[TD]Mr. Manager[/TD]
[TD]200,000[/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="class: cms_table_grid, width: 500"]
<tbody>[TR]
[TD]200[/TD]
[TD]Atlanta[/TD]
[TD]Dan Davidson[/TD]
[TD]Ms. Supervisor[/TD]
[TD]300,000[/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="class: cms_table_grid, width: 500"]
<tbody>[TR]
[TD]200[/TD]
[TD]Atlanta[/TD]
[TD]Dan Davidson[/TD]
[TD]Ms. Supervisor[/TD]
[TD]300,000[/TD]
[/TR]
</tbody>[/TABLE]


etc.


Code:
Sub Split_Cr()
Dim rng As Range, cel As Range, cr%
Set rng = Range([C2], Cells(Rows.Count, "C").End(xlUp))
For Each cel In rng
    cr = Len(cel) - Len(Replace(cel, vbLf, ""))
    If cr > 0 Then
        cel(2).Resize(cr).EntireRow.Insert
        cel.Resize(cr + 1) = WorksheetFunction.Transpose(Split(cel, vbLf))
    
    Set rng = Range([A2], Cells(Rows.Count, "C").End(xlUp)(1, 8))
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
rng = rng.Value
    
    End If


Next


End Sub






Code:
Sub Split_Cr()
Dim rng As Range, cel As Range, cr%
Set rng = Range([C2], Cells(Rows.Count, "C").End(xlUp))
For Each cel In rng
    cr = Len(cel) - Len(Replace(cel, vbLf, ""))
    If cr > 0 Then
        cel(2).Resize(cr).EntireRow.Insert
        cel.Resize(cr + 1) = WorksheetFunction.Transpose(Split(cel, vbLf))
    End If
Next
Set rng = Range([A2], Cells(Rows.Count, "C").End(xlUp)(1, 3))
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
rng = rng.Value
End Sub
 
Upvote 0
Don't do this : "I also moved the entire rng portion inside the If statement."

Code:
Sub Split_Cr()
Dim rng As Range, cel As Range, cr%
Set rng = Range([C2], Cells(Rows.Count, "C").End(xlUp))
For Each cel In rng
    cr = Len(cel) - Len(Replace(cel, vbLf, ""))
    If cr > 0 Then
        cel(2).Resize(cr).EntireRow.Insert
        cel.Resize(cr + 1) = WorksheetFunction.Transpose(Split(cel, vbLf))
    End If
Next
Set rng = Range([A2], Cells(Rows.Count, "C").End(xlUp)(1, [COLOR=#ff0000]8[/COLOR]))
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
rng = rng.Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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