Turn one row into multiple rows in Excel
I have multiple items that are available in multiple distribution centers (i.e., a many-to-many relationship). There is currently one row per item, with one column for each distribution center. A cell in the row for item X and the column for distribution center Y is marked with the code for distribution center Y if item X is available there and blank otherwise. An item with multiple distribution centers will have multiple distribution center codes (in their respective columns). So the current sheet looks like:
| A | B |*| S-AJ |
1 | ID # | Description |…| Distribution Centers |
2 | 17 | Ginkgo Biloba |…| | | | | | | SE |
3 | 42 | Ginseng |…| | MP | MS | | NW | | |
︙
Columns C
through R
contain other attributes of the items, such as UPC code, cost, and price, which are not relevant to this question. My actual sheet has 18 distribution centers, spanning columns S
through AJ
; I reduced that to get the example to fit into Stack Exchange’s window.
I need to have a single distribution center column, with a single distribution code per row, and then duplicate the rows as needed for items that currently contain multiple codes. The result should look like:
| A | B |*| S |
1 | ID # | Description |…| DC |
2 | 17 | Ginkgo Biloba |…| SE |
3 | 42 | Ginseng |…| MP |
4 | 42 | Ginseng |…| MS |
5 | 42 | Ginseng |…| NW |
︙
where cells A3:R3
, A4:R4
, and A5:R5
, contain the same information.
The only way I can think of doing this, which would be time consuming, would be to copy the item number into multiple rows; and in the column that has the distribution code I would change code for the item that is available in each distribution center. I will be doing this for 900 items. Is there an easier way to do this?
- Create a new sheet.
Copy the header row(s), the column widths, and the formats,
except do not copy Columns
T
-AJ
. It may be easiest to copy the entire sheet, then delete all rows other than 1 and unmerge ColumnsS
-AJ
. -
First we want to replicate each item row from
Sheet1
ontoSheet2
18 times — once for each distribution center. Type=INDEX(Sheet1!A:A, INT((ROW()-2)/18)+2, 1) & ""
inSheet2!A2
.INT((ROW()-2)/18)+2
maps rows 2-19 onSheet2
to row 2 onSheet1
, rows 20-37 onSheet2
to row 3 onSheet1
, etc. The& ""
causes Excel to display a blank when referencing a blank cell inSheet1
. If you don’t have any blanks inSheet1
, you can leave this off. If you don’t like this particular solution, you can use one of the other solutions from Display Blank when Referencing Blank Cell in Excel.Drag/fill this to the right, to cell
R2
. - Enter
=INDEX(Sheet1!$S:$AJ, INT((ROW()-2)/18)+2, MOD(ROW()-2, 18)+1)
intoSheet2!S2
. This references the same row inSheet1
as the above formula, butSheet2!S2
gets the value fromSheet1!S2
,Sheet2!S3
gets the value fromSheet1!T2
,Sheet2!S4
gets the value fromSheet1!U2
, etc. This will display0
s for blanks. - Select the entire row
A2:S2
and drag/fill down to get all your data. This will need to be 18 times as many rows as you have onSheet1
; i.e., 18×900=16200. - Copy all of
Sheet2
and paste values. - Filter Column
S
. Display only the zeroes. Delete all the rows (other than row 1). Remove the filter.
Done.