1

I have a table similar to this:

ID | Item 1 | Item 2 | Item 3 | Qty 1 | Qty 2 | Qty 3
1  |        |    X   |    X   |       |   3   |   4 
2  |    X   |        |    X   |   2   |       |   1
3  |        |        |    X   |       |       |   9

I need to find a way to create a new row for each item per ID:

ID | Item | Qty
1  |   2  |  3
1  |   3  |  4
2  |   1  |  2

Is there any way to accomplish this either through formula, native Excel tools, or VBA?

Community
  • 1
  • 1
MBrewer
  • 81
  • 2
  • 9
  • So far, I've tried to transpose and attempted to create an if statement to lookup and place in the cell only if the cell had data. – MBrewer Oct 09 '15 at 16:34
  • =IF('1stCut'!B2<>"",'1stCut'!B2,(IF('1stCut'!C2<>"",'1stCut'!C2,(IF('1stCut'!D2<>"",'1stCut'!D2,(IF('1stCut'!E2<>"",'1stCut'!E2,(IF('1stCut'!F2<>"",'1stCut'!F2,(IF('1stCut'!G2<>"",'1stCut'!G2,(IF('1stCut'!H2<>"",'1stCut'!H2,(IF('1stCut'!I2<>"",'1stCut'!I2,(IF('1stCut'!J2<>"",'1stCut'!J2,(IF('1stCut'!K2<>"",'1stCut'!K2,(IF('1stCut'!L2<>"",'1stCut'!L2,(IF('1stCut'!M2<>"",'1stCut'!M2,(IF('1stCut'!N2<>"",'1stCut'!N2,(IF('1stCut'!O2<>"",'1stCut'!O2,(IF('1stCut'!P2<>"",'1stCut'!P2,(IF('1stCut'!Q2<>"",'1stCut'!Q2,(IF('1stCut'!R2<>"",'1stCut'!R2,""))))))))))))))))))))))))))))))))) – MBrewer Oct 09 '15 at 16:34
  • So you are saying the amount of rows per ID in the second table is not static? Otherwise, a Vlookup would be fine. – Yaegz Oct 09 '15 at 16:43
  • Correct, The ID needs to repeat for each present Item value. – MBrewer Oct 09 '15 at 16:52
  • I think the simplest wording is: For every value found in a cell in a row, create a new row containing the value – MBrewer Oct 09 '15 at 16:58
  • Ok yeah that is more complicated. It sounds like VBA is the way to go. Would you want to update table 2 based on table 1 on the click of a button or automatically? – Yaegz Oct 09 '15 at 17:43
  • Ideally, yes. Columns B:R will always be our items, columns S:AI will always be our quantities. – MBrewer Oct 09 '15 at 17:55

2 Answers2

2

Use the technique described in detail here to get a Table that looks like this:

SO33043100 first example

Delete where ColumnC is (Blanks) or X. Select ColumnB and HOME > Editing - Find & Select, Replace..., Find what: Qty, Replace All. Adjust column labels and center all. You should then have a Table like so:

SO33043100 second example

Which you could choose to convert to Rage by right-clicking on one of its cells, Table, Convert to Range.

Community
  • 1
  • 1
pnuts
  • 58,317
  • 11
  • 87
  • 139
  • Yeah I think this is the best way to do it. I didn't know about alt + D+ P. You can also just filter out x and blanks in column C instead of deleting them if you want. – Yaegz Oct 09 '15 at 18:15
  • 1
    Thanks pnuts! This gave me what I needed. – MBrewer Oct 09 '15 at 19:37
1

You could use Microsoft Query to point to your data sheet. Select your data and then give it a named range by typing a name in the box to the left of the Formula bar at the top.

Save your workbook to a directory somewhere. Then to choose it as a data source:

  • Click the data tab at the top.
  • Select "From Other Sources" button.
  • Select "From Microsoft Query" from the menu.
  • Choose the "Excel Files" data source.
  • Find and select your data file that you saved the workbook as.
  • If you get an error that no tables were found, click ok and then select "Options..." button at the bottom and make sure to check to show all types of tables.
  • Select the table or sheet that your data is on and move all columns over to the right side box and then click next.
  • click next again on the filter dialog box.
  • click next again on the sort dialog box.
  • Select View data or edit query in Microsoft Query and click Finish.
  • Click the view menu in Microsoft Query and select "SQL..."
  • Enter this SQL statement (and change the directory path to match where you saved your file:


    SELECT `Sheet1$`.ID, 
    `Sheet1$`.Qty1
    FROM `C:\Users\MyName\Desktop\data.xlsx`.`Sheet1$` `Sheet1$`
    WHERE (`Sheet1$`.Item1='x')
    union
    SELECT `Sheet1$`.ID, 
    `Sheet1$`.Qty2
    FROM `C:\Users\MyName\Desktop\data.xlsx`.`Sheet1$` `Sheet1$`
    WHERE (`Sheet1$`.Item2='x')
    union
    SELECT `Sheet1$`.ID, 
    `Sheet1$`.Qty3
    FROM `C:\Users\MyName\Desktop\data.xlsx`.`Sheet1$` `Sheet1$`
    WHERE (`Sheet1$`.Item3='x')
  • Click Ok
  • Select the file menu and then "Return Data to Microsoft Excel".
  • Select the destination cell for your data and click OK.
Brian Pressler
  • 6,653
  • 2
  • 19
  • 40
  • Able to get to the goal through both solutions... thanks as well, Brian! – MBrewer Oct 09 '15 at 19:44
  • Your welcome... glad to help. :) One advantage to this method to keep in mind is that if you need to change your data, you can just edit your data sheet, right click your result table, and select "Refresh" to refresh instantly. You won't need to go though all the steps anymore to reproduce your output. – Brian Pressler Oct 09 '15 at 20:56