Transpose every set of 5 rows into 1 row

howdedo

New Member
Joined
May 21, 2009
Messages
2
I have a large data set where a single account is represented as a fixed number of rows (i.e. 5) in a single column. In other words, the first 5 rows in the column represent a single account. The next 5 rows represent the next account, etc.

I want to convert each set of 5 rows into a single row with 5 colums so that I can sort and aggregate the data. How can I convert every 5 rows into 5 columns in a single row?

For example, I want to convert this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

into this:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Is this possible to do this in a manner that could be easily be repeated to accomodate 10-15 thousand rows of input data in this format?

Thanks for your help.
Tom
 
[TABLE="width: 512"]
<tbody>[TR]
[TD="align: right"]1[/TD]
[TD="width: 64"][/TD]
[TD="class: xl63, width: 64, align: right"]1[/TD]
[TD="class: xl63, width: 64, align: right"]1[/TD]
[TD="class: xl63, width: 64, align: right"]1[/TD]
[TD="class: xl63, width: 64, align: right"]1[/TD]
[TD="class: xl63, width: 64, align: right"]1[/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD][/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD][/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]4[/TD]
[TD][/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]8[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]9[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]10[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]11[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]12[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]13[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]14[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]15[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]16[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
This is what I get. Seems like I can not get it to work.
=INDEX($A$2:$A$16,ROWS(C$2:C2)*5-5+COLUMNS($C2:C2)) I used the formula. What am I doing wrong?
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Your formula seems fine. Although you'll probably want to extent the indexed range...

Code:
=INDEX([COLOR=#ff0000]$A$2:$A$21[/COLOR],ROWS(C$2:C2)*5-5+COLUMNS($C2:C2))

Check to make sure that the calculation mode is set to "Automatic"...

Code:
Ribbon > Formulas > Calculation > Calculation Options > Automatic

Hope this helps!
 
Upvote 0
Re: Transpose every set of 2 rows into 1 row

Hi,

I have a layout like this with several thousand rows

1 9 7ABCB 8458 1234123 00
2 9 3453453 345345 345345 345345 345345 345345 345345 345345 345345 345345
1 9 7ABCB 8458 1234123 00
2 9 3453453 345345 345345 345345 345345 345345 345345 345345 345345 345345

My resultant layout would need to display record type 1 and 2 (each pair) as 1 row

How would the above formula need to be tweaked to achieve this?

thanks
 
Upvote 0
Re: Transpose every set of 2 rows into 1 row

Try the following...

A1:L4

[TABLE="width: 768"]
<colgroup><col style="width:48pt" span="12" width="64"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 64, align: right"]1[/TD]
[TD="class: xl63, width: 64, align: right"]9[/TD]
[TD="class: xl63, width: 64"]7ABCB[/TD]
[TD="class: xl63, width: 64, align: right"]8458[/TD]
[TD="class: xl63, width: 64, align: right"]1234123[/TD]
[TD="class: xl63, width: 64, align: right"]0[/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"] [/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2[/TD]
[TD="class: xl63, align: right"]9[/TD]
[TD="class: xl63, align: right"]3453453[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63, align: right"]9[/TD]
[TD="class: xl63"]7ABCB[/TD]
[TD="class: xl63, align: right"]8458[/TD]
[TD="class: xl63, align: right"]1234123[/TD]
[TD="class: xl63, align: right"]0[/TD]
[TD="class: xl63"] [/TD]
[TD="class: xl63"] [/TD]
[TD="class: xl63"] [/TD]
[TD="class: xl63"] [/TD]
[TD="class: xl63"] [/TD]
[TD="class: xl63"] [/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2[/TD]
[TD="class: xl63, align: right"]9[/TD]
[TD="class: xl63, align: right"]3453453[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[TD="class: xl63, align: right"]345345[/TD]
[/TR]
</tbody>[/TABLE]

N1, copied down and across to Column S:

=INDEX($A$1:$F$4,ROWS(N$1:N1)*2-2+1,COLUMNS($N1:N1))

T1, copied down and across to Column AE:

=INDEX($A$1:$L$4,ROWS(T$1:T1)*2,COLUMNS($T1:T1))

Hope this helps!
 
Upvote 0
Actually I am trying to do the same thing except with 12 rows at a time but with a variable number of rows every time data is entered. Can be done using the above formula?

Brand new to the site, trying to teach myself VBA to help my group at work in manipulating data from multiple instruments that export using Excel.......only been doing for two weeks but already having trouble.

Thanks ahead of time for any help.
 
Upvote 0
Actually I am trying to do the same thing except with 12 rows at a time but with a variable number of rows every time data is entered. Can be done using the above formula?

Brand new to the site, trying to teach myself VBA to help my group at work in manipulating data from multiple instruments that export using Excel.......only been doing for two weeks but already having trouble.

Thanks ahead of time for any help.

Try starting a new thread and someone will likely be able to provide you with a solution...
 
Upvote 0
Re: Transpose every set of 2 rows into 1 row

Thank you!! This worked for me.

In the data, there is a possibility to have repeated 1's without a 2 succeeding it (repeated record types as shown below). When this is the case, all the stand alone 1's will not have anything beyond column F. As long as every 1 has a succeeding 2, these formulas display data correctly. When stand alone 1's get in between sets of 1&2 the result gets irregular. Is there any way that can be achieved??

1 9 7ABCB 8458 1234123 0
2 9 3453453 345345 345345 345345 345345 345345 345345 345345 345345 345345
1 9 7ABCB 8458 1234123 0
1 9 7ABCB 8458 1234123 0
1 9 7ABCB 8458 1234123 0
2 9 3453453 345345 345345 345345 345345 345345 345345 345345 345345 345345

Thanks much for helping with this...
 
Upvote 0
Re: Transpose every set of 2 rows into 1 row

In that case, assuming that the sheet containing the data is the active sheet, the first row contains the column headers, and the data starts at Row 2, try the following macro that needs to be placed in a regular module (Alt+F11 > Insert > Module > Copy/Paste > Alt+Q)...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] RearrangeData()

    [COLOR=darkblue]Dim[/COLOR] vData [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] aResults() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] c [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] j [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    [COLOR=darkblue]If[/COLOR] LastRow = 1 [COLOR=darkblue]Then[/COLOR]
        MsgBox "No data found.", vbExclamation
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    vData = Range("A2:L" & LastRow)
    
    [COLOR=darkblue]ReDim[/COLOR] aResults(1 [COLOR=darkblue]To[/COLOR] 18, 1 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](vData, 1))
    
    c = 0
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](vData)
        [COLOR=darkblue]If[/COLOR] vData(i, 1) = 1 [COLOR=darkblue]Then[/COLOR]
            c = c + 1
            [COLOR=darkblue]For[/COLOR] j = 1 [COLOR=darkblue]To[/COLOR] 6
                aResults(j, c) = vData(i, j)
            [COLOR=darkblue]Next[/COLOR] j
        [COLOR=darkblue]ElseIf[/COLOR] vData(i, 1) = 2 [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]If[/COLOR] i = 1 [COLOR=darkblue]Then[/COLOR] c = 1
            [COLOR=darkblue]For[/COLOR] j = 7 [COLOR=darkblue]To[/COLOR] 18
                aResults(j, c) = vData(i, j - 6)
            [COLOR=darkblue]Next[/COLOR] j
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] i
    
    [COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] aResults(1 To 18, 1 To c)
    
    Worksheets.Add
    Range("A2").Resize(UBound(aResults, 2), 18).Value = Application.Transpose(aResults)
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0
Thank you for this this formula and advice.

How can I preserve the spaces between the original rows when the data is transposed?

I get the top result with this formula (adjusted for every 4 rows), but I want the bottom result (pictured below).

The reason is that I have data in other columns that is only populated every 4 rows and relates to values 1, 4, 8, etc. that I want to align in the same row so I can then proceed to use pivot tables from there.

[TABLE="width: 384"]
<tbody>[TR]
[TD="class: xl65, width: 64"]value[/TD]
[TD="class: xl65, width: 64"][/TD]
[TD="class: xl65, width: 64"]x[/TD]
[TD="class: xl65, width: 64"]y[/TD]
[TD="class: xl65, width: 64"]z[/TD]
[TD="class: xl65, width: 64"]xy[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"] 1[/TD]
[TD="class: xl64"]2[/TD]
[TD="class: xl64"]3[/TD]
[TD="class: xl64"]4[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]6[/TD]
[TD="class: xl64"]7[/TD]
[TD="class: xl64"]8[/TD]
[TD="class: xl64"]9[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]3[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]11[/TD]
[TD="class: xl64"]12[/TD]
[TD="class: xl64"]0[/TD]
[TD="class: xl64"]0[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]4[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]5[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]6[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]7[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]8[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]9[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]10[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]11[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]12[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[TD="class: xl64"]#REF![/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="class: xl65"]value[/TD]
[TD="class: xl65"][/TD]
[TD="class: xl65"]x[/TD]
[TD="class: xl65"]y[/TD]
[TD="class: xl65"]z[/TD]
[TD="class: xl65"]xy[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"] 1[/TD]
[TD="class: xl64"]2[/TD]
[TD="class: xl64"]3[/TD]
[TD="class: xl64"]4[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]3[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]4[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]5[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]5[/TD]
[TD="class: xl64"]6[/TD]
[TD="class: xl64"]7[/TD]
[TD="class: xl64"]8[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]6[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]7[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]8[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]9[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"]9[/TD]
[TD="class: xl64"]10[/TD]
[TD="class: xl64"]11[/TD]
[TD="class: xl64"]12[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]10[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]11[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]12[/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[TD="class: xl63"][/TD]
[/TR]
</tbody>[/TABLE]


Thank you
 
Upvote 0
I have the same problem but I want to transpose every set of 7 rows keeping the formatted (Color) cells.. is this possible???
thanks!
Karina
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

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