Rearanging complex excel data

0

I have a complex set of data that I need to rearrange in excel.

The table looks something like this

+----+--------+--------+--------+--------+
| ID | Text A | Text B | Text C | Text D |
+----+--------+--------+--------+--------+
| 1  | xx     |        | yy     | zz     |
+----+--------+--------+--------+--------+
| 2  |        | aa     | bb     |        |
+----+--------+--------+--------+--------+
| 3  | cc     |        |        | dd     |
+----+--------+--------+--------+--------+

The result I need to output is essentially a combination of a transpose and a coalesce. But I have no idea how to tackle this problem in Excel.

+----+------+
| ID | Text |
+----+------+
| 1  | xx   |
+----+------+
| 1  | yy   |
+----+------+
| 1  | zz   |
+----+------+
| 2  | aa   |
+----+------+
| 2  | bb   |
+----+------+
| 3  | cc   |
+----+------+
| 3  | dd   |
+----+------+

Bluephlame

Posted 2018-03-06T10:19:34.847

Reputation: 133

1

Possible duplicate of Excel 2007 transpose/combine multiple rows into one

– Máté Juhász – 2018-03-06T10:22:48.047

reference question gives you a good start, after performing unpivot all you need is to delete extra column and remove rows with blank cell in second column. – Máté Juhász – 2018-03-06T10:23:34.877

Possible duplicate of Turn one row into multiple rows in Excel

– G-Man Says 'Reinstate Monica' – 2018-03-07T03:48:18.063

Answers

0

This assumes that the original data is in Sheet1 and the output will be placed in Sheet2.

Running this short macro:

Sub ReOrganizer()
    Dim N As Long, nCols As Long
    Dim i As Long, j As Long, k As Long
    Dim s1 As Worksheet, s2 As Worksheet

    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    N = s1.Cells(Rows.Count, 1).End(xlUp).Row
    nCols = 5
    k = 2

    For i = 2 To N
        Item = s1.Cells(i, 1)
        For j = 2 To nCols
            If s1.Cells(i, j) <> "" Then
                s2.Cells(k, 1) = Item
                s2.Cells(k, 2) = s1.Cells(i, j)
                k = k + 1
            End If
        Next j
    Next i
End Sub

will take data like this in Sheet1:

enter image description here

and produce this in Sheet2:

enter image description here

Gary's Student

Posted 2018-03-06T10:19:34.847

Reputation: 15 540