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
 
111111
211111
311111
411111
5
6
7
8
9
10
11
12
13
14
15
16

<tbody>
</tbody>
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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

197ABCB845812341230
293453453345345345345345345345345345345345345345345345345345345
197ABCB845812341230
293453453345345345345345345345345345345345345345345345345345345

<colgroup><col style="width:48pt" span="12" width="64"> </colgroup><tbody>
</tbody>

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.

valuexyzxy
1 1234
26789
3111200
4#REF!#REF!#REF!#REF!
5#REF!#REF!#REF!#REF!
6#REF!#REF!#REF!#REF!
7#REF!#REF!#REF!#REF!
8#REF!#REF!#REF!#REF!
9#REF!#REF!#REF!#REF!
10#REF!#REF!#REF!#REF!
11#REF!#REF!#REF!#REF!
12#REF!#REF!#REF!#REF!
valuexyzxy
1 1234
2
3
4
55678
6
7
8
99101112
10
11
12

<tbody>
</tbody>


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,214,587
Messages
6,120,405
Members
448,958
Latest member
Hat4Life

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