Transpose irregular data from columns to rows

slow1911s

New Member
Joined
May 12, 2012
Messages
6
I have a two column set of data with ZIP codes in one and order numbers in the second. In some ZIP codes there is only one order number, but some have two and up to six.

I have used the Copy/Paste Special-Transpose, but this worksheet has hundreds of rows and is quite time consuming. Is there another way I'm not considering?

It looks like this:

What I have
[TABLE="class: grid, width: 150"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]12345[/TD]
[TD]Order1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Order2[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD]Order3[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]45678[/TD]
[TD]Order4[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]98765[/TD]
[TD]Order5[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]Order6[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]34567[/TD]
[TD]Order7[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]13579[/TD]
[TD]Order8[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD]Order9[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD]Order10[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD]Order11[/TD]
[/TR]
</tbody>[/TABLE]

What I need
[TABLE="class: grid, width: 150"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]12345[/TD]
[TD]Order1[/TD]
[TD]Order2[/TD]
[TD]Order3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]45678[/TD]
[TD]Order4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]98765[/TD]
[TD]Order5[/TD]
[TD]Order6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]34567[/TD]
[TD]Order7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]13579[/TD]
[TD]Order8[/TD]
[TD]Order9[/TD]
[TD]Order10[/TD]
[TD]Order11[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this I have used 2 helper columns though:


Excel 2003
ABCDE
1NumberOrderHelper 1Helper 2
212345Order 11234512345 - 1
3Order 21234512345 - 2
4Order 31234512345 - 3
545678Order 14567845678 - 1
6Order 24567845678 - 2
7Order 34567845678 - 3
834567Order 13456734567 - 1
9Order 23456734567 - 2
10Order 33456734567 - 3
11Order 43456734567 - 4
12
13
14
15
16Final Result
17Number
1812345Order 1Order 2Order 3#N/A
1945678Order 1Order 2Order 3#N/A
2034567Order 1Order 2Order 3Order 4
Sheet1
Cell Formulas
RangeFormula
C2=IF(COUNTIF(A2:A2,A2)=1,A2,C1)
C3=IF(COUNTIF(A3:A3,A3)=1,A3,C2)
C4=IF(COUNTIF(A4:A4,A4)=1,A4,C3)
C5=IF(COUNTIF(A5:A5,A5)=1,A5,C4)
C6=IF(COUNTIF(A6:A6,A6)=1,A6,C5)
C7=IF(COUNTIF(A7:A7,A7)=1,A7,C6)
C8=IF(COUNTIF(A8:A8,A8)=1,A8,C7)
C9=IF(COUNTIF(A9:A9,A9)=1,A9,C8)
C10=IF(COUNTIF(A10:A10,A10)=1,A10,C9)
C11=IF(COUNTIF(A11:A11,A11)=1,A11,C10)
C18=INDEX($B$2:$B$11,MATCH($A18&" - "&COLUMNS($B17:C17),$D$2:$D$11,0))
C19=INDEX($B$2:$B$11,MATCH($A19&" - "&COLUMNS($B18:C18),$D$2:$D$11,0))
C20=INDEX($B$2:$B$11,MATCH($A20&" - "&COLUMNS($B19:C19),$D$2:$D$11,0))
D2=C2&" - "&COUNTIF(C$2:C2,C2)
D3=C3&" - "&COUNTIF(C$2:C3,C3)
D4=C4&" - "&COUNTIF(C$2:C4,C4)
D5=C5&" - "&COUNTIF(C$2:C5,C5)
D6=C6&" - "&COUNTIF(C$2:C6,C6)
D7=C7&" - "&COUNTIF(C$2:C7,C7)
D8=C8&" - "&COUNTIF(C$2:C8,C8)
D9=C9&" - "&COUNTIF(C$2:C9,C9)
D10=C10&" - "&COUNTIF(C$2:C10,C10)
D11=C11&" - "&COUNTIF(C$2:C11,C11)
D18=INDEX($B$2:$B$11,MATCH($A18&" - "&COLUMNS($B17:D17),$D$2:$D$11,0))
D19=INDEX($B$2:$B$11,MATCH($A19&" - "&COLUMNS($B18:D18),$D$2:$D$11,0))
D20=INDEX($B$2:$B$11,MATCH($A20&" - "&COLUMNS($B19:D19),$D$2:$D$11,0))
B18=INDEX($B$2:$B$11,MATCH($A18&" - "&COLUMNS($B17:B17),$D$2:$D$11,0))
B19=INDEX($B$2:$B$11,MATCH($A19&" - "&COLUMNS($B18:B18),$D$2:$D$11,0))
B20=INDEX($B$2:$B$11,MATCH($A20&" - "&COLUMNS($B19:B19),$D$2:$D$11,0))
E18=INDEX($B$2:$B$11,MATCH($A18&" - "&COLUMNS($B17:E17),$D$2:$D$11,0))
E19=INDEX($B$2:$B$11,MATCH($A19&" - "&COLUMNS($B18:E18),$D$2:$D$11,0))
E20=INDEX($B$2:$B$11,MATCH($A20&" - "&COLUMNS($B19:E19),$D$2:$D$11,0))
 
Upvote 0
I have a two column set of data with ZIP codes in one and order numbers in the second. In some ZIP codes there is only one order number, but some have two and up to six.

I have used the Copy/Paste Special-Transpose, but this worksheet has hundreds of rows and is quite time consuming. Is there another way I'm not considering?

It looks like this:

What I have
...
Would you be happy to use a VBA macro?
Code:
Sub transpose_irregular()
Dim a, c(), p&, q&, i&
a = Cells(1).CurrentRegion.Resize(, 2)
ReDim c(1 To UBound(a, 1), 1 To 2)
For i = 1 To UBound(a, 1)
    If Len(a(i, 1)) > 0 Then
        p = p + 1: q = 2
        c(p, 1) = a(i, 1)
        c(p, 2) = a(i, 2)
    Else
        q = q + 1
        If q > UBound(c, 2) Then _
            ReDim Preserve c(1 To UBound(a, 1), 1 To q)
        c(p, q) = a(i, 2)
    End If
Next i
Cells(1).CurrentRegion.ClearContents
Cells(1).Resize(p, UBound(c, 2)) = c
For i = 1 To UBound(c, 2)
    Cells(1, i) = Chr(i + 64)
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top