Solved

# Create duplicate rows based on criteria from one column

Posted on 2014-12-11
250 Views
I have a worksheet with seven columns.  The seventh column has values separated by commas that belong to the same row (columns A - F).  I would to take each value from the seventh column and create a new row adding the values from columns A - F for each row.  I have attached an example with one of the rows.
Excel-Example.xlsx
0
Question by:amergts

LVL 29

Expert Comment

ID: 40494651
What you need to do would be to read the cells A through G into separate variables. Use the Split function, or even better the SplitEx (http://www.cpearson.com/excel/Split.aspx) function to break up your string into an array. Then write the values back to the sheet (I suggest a different sheet than the one you started on).

Leon
0

LVL 31

Expert Comment

ID: 40494654
How big is your true data set? Would it be too cumbersome to insert the additional rows manually?

If not you can then use Text to columns to split your column G values, copy paste transpose to fill the new rows and then the blank cells ( A - F ) can be filled using the filter function to show blank rows and populate with a simple formula linking the cells to the row above.

Are the values in column G consistent in quantity and in length?

Thanks
Rob H
0

LVL 45

Accepted Solution

Martin Liss earned 500 total points
ID: 40494912
Here's a working macro.
``````Sub SplitLines()
Dim lngRow As Long
Dim strParts() As String
Dim lngPart As Long
Dim lngLastRow As Long

With ActiveSheet
lngLastRow = .Range("A1048576").End(xlUp).Row
For lngRow = lngLastRow To 2 Step -1
strParts = Split(.Cells(lngRow, 7), ",")
.Cells(lngRow, 7) = strParts(UBound(strParts))
For lngPart = UBound(strParts) - 1 To 0 Step -1
.Rows(lngRow & ":" & lngRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("A" & lngRow & ":F" & lngRow).Value = .Range("A" & lngRow + 1 & ":F" & lngRow + 1).Value
.Cells(lngRow, 7) = strParts(lngPart)
Next
Next
End With
``````
0

Author Closing Comment

ID: 40495037
Thanks Martin!  Incredible!
0

LVL 45

Expert Comment

ID: 40495050
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

## Featured Post

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…