-1

Does anyone know of a way to get Excel to do what is shown in the below example

A         |     B
================================
Apple     |     Pie
Apple     |     Sauce
Apple     |     Juice
Banana    |     Smoothie
Banana    |     Split

into this structure?

A        |    B         |     C      |    D      | 
==================================================
Apple    |   Pie        |    Sauce   |   Juice   |
Banana   |   Smoothie   |    Split   |           |

I have been searching online and can't find a supported way so far, I want to know if there is a supported way first before I try writing some VBA code to do it.

The usual paste special > transpose does not give this format, because it is not supposed to.

I obviously want to automate this for a large dataset.

4

2 回答 2

2

I would go with VBA on this one but if you want a non VBA solution then see this. This is something which I came up quickly with and I am sure there could be better formulas to achieve the same thing.

D2:D6 values can be got from a Pivot on Col A

E2 has a formula

=IF(OFFSET(INDIRECT(CELL("address",INDEX($A$2:$A$16,MATCH(D2,$A$2:$A$16,0),1))),0,1)=0,"",OFFSET(INDIRECT(CELL("address",INDEX($A$2:$A$16,MATCH(D2,$A$2:$A$16,0),1))),0,1))

F2 has a formula

=IF(OFFSET(INDIRECT(CELL("address",INDEX($A$2:$A$16,MATCH(D2,$A$2:$A$16,0),1))),1,1)=0,"",OFFSET(INDIRECT(CELL("address",INDEX($A$2:$A$16,MATCH(D2,$A$2:$A$16,0),1))),1,1))

G2 has a formula

=IF(OFFSET(INDIRECT(CELL("address",INDEX($A$2:$A$16,MATCH(D2,$A$2:$A$16,0),1))),2,1)=0,"",OFFSET(INDIRECT(CELL("address",INDEX($A$2:$A$16,MATCH(D2,$A$2:$A$16,0),1))),2,1))

Now simply copy the formulas down.

enter image description here

于 2013-10-22T20:19:53.753 回答
2

And if you want to try a macro, give this a try:

Sub TwoDimension()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim N As Long, i As Long, K As Long
    Dim ii As Long
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    s1.Range("A1:B1").Copy s2.Range("A1:B1")
    N = s1.Cells(Rows.Count, "A").End(xlUp).Row
    K = 3
    ii = 1
    For i = 2 To N
        If s1.Cells(i, 1) = s1.Cells(i - 1, 1) Then
            s2.Cells(ii, K) = s1.Cells(i, 2)
            K = K + 1
        Else
            ii = ii + 1
            s2.Cells(ii, 1) = s1.Cells(i, 1)
            s2.Cells(ii, 2) = s1.Cells(i, 2)
            K = 3
        End If
    Next i
End Sub

The data will be re-formatting on a separate worksheet.

于 2013-10-22T20:23:41.390 回答