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?

Location IDLocationEmployeesManagerBudget
100ChicagoAlex Anderson

Bob Barney

Chris Conners
Mr. Manager200,000
200AtlantaDan Davidson

Emmit Erickson

Fran Fredrickson
Ms. Supervisor300,000

<tbody>
</tbody>


Location IDLocationEmployeeManagerBudget
100
ChicagoAlex AndersonMr. Manager200,000
100ChicagoBob BarneyMr. Manager200,000
100ChicagoChris ConnersMr. Manager200,000
200AtlantaDan DavidsonMs. Supervisor300,000
200AtlantaEmmit EricksonMs. Supervisor300,000
200AtlantaFran FredricksonMs. Supervisor300,000

<tbody>
</tbody>

Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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:

Location IDLocationEmployeesManagerBudget
100ChicagoAlex Anderson

Bob Barney

Chris Conners
Mr. Manager200,000
200AtlantaDan Davidson

Emmit Erickson

Fran Fredrickson
Ms. Supervisor300,000

<tbody>
</tbody>



Location IDLocationEmployeeManagerBudget
100ChicagoAlex AndersonMr. Manager200,000
100ChicagoAlex AndersonMr. Manager200,000
100ChicagoBob Barney
Mr. Manager200,000
100ChicagoBob BarneyMr. Manager200,000
100ChicagoChris ConnersMr. Manager200,000
100ChicagoChris ConnersMr. Manager200,000

<tbody>
</tbody>

200AtlantaDan DavidsonMs. Supervisor300,000

<tbody>
</tbody>

200AtlantaDan DavidsonMs. Supervisor300,000

<tbody>
</tbody>


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,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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