Traspose each row every 5 rows (of 15 cells) into one column

Nataly92

New Member
Joined
Oct 19, 2013
Messages
2
I am using Microsoft Office Excel 2007 and Windows 7.
I have a lot of data (1-4 thousands of rows) where each column represents a year for 15 years. Every set of 5 rows are different data for a company and the values for every year of each data are under the representative column.

I want to transpose the values for every year, of one data at a time, into one column. Then do the same for the next data.

For Example I have something like this:

199819992000200120022003200420052006200720082009201020112012
comp.1data1123456789101112131415
comp.1data2161718192021222324252627282930
comp.1data3313233343536373839404142434445
comp.1data4464748495051525354555657585960
comp.1data5616263646566676869707172737475
comp.2data1767778798081828384858687888990
comp.2data2919293949596979899100101102103104105
comp.2data3106107108109110111112113114115116117118119120
comp.2data4121122123124125126127128129130131132133134135
comp.2data5136137138139140141142143144145146147148149150
comp.3data1151152153154155156157158159160161162163164165
comp.3data2166167168169170171172173174175176177178179180
comp.3data3181182183184185186187188189190191192193194195
comp.3data4196197198199200201202203204205206207208209210
comp.3data5211212213214215216217218219220221222223224225

<tbody>
</tbody>

And I want to make it into like this:

data1data2data3data4data5
116314661
217324762
318334863
419344964
520355065
621365166
722375267
823385368
924395469
1025405570
1126415671
1227425772
1328435873
1429445974
1530456075
7691106121136
7792107122137
7893108123138
7994109124139
8095110125140
8196111126141
8297112127142
8398113128143
8499114129144
85100115130145
86101116131146
87102117132147
88103118133148
89104119134149
90105120135150
151166181196211
152167182197212
153168183198213
154169184199214
155170185200215
156171186201216
157172187202217
158173188203218
159174189204219
160175190205220
161176191206221
162177192207222
163178193208223
164179194209224
165180195210225

<tbody>
</tbody>

Sorry for the big example. If there is a way to do this for one data (i.e. data1) it will be easy to do it for the other 4. I also know how to create a macro from the Developer Tab if it is easier to create a vba code. A formula of course is ok too. I tried for hours to find a way to do this but I couldn't...
Thank you very much for your help,
Nataly.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Run this macro with the data sheet as the active sheet and change the output sheet name (hightlighted in red) to whatever sheet name (other than your data sheet) that you want the transposed data to go to....

Rich (BB code):
Sub TransposeData()
  Dim X As Long, R As Long
  R = 2
  With Sheets("Sheet2")
    .Range("A1:E1").Value = WorksheetFunction.Transpose(Range("A2:A6").Value)
    For X = 2 To Cells(Rows.Count, "A").End(xlUp).Row Step 5
      .Cells(R, 1).Resize(15, 5) = WorksheetFunction.Transpose(Cells(X, "B").Resize(5, 15).Value)
      R = R + 5
    Next
  End With
End Sub
 
Upvote 0
I tried the code but there were a couple of problems which I fixed them in a way. The traspose was made only for the 5 first years. I thought that because the rows for each company were 5 and the years were 15, that the last 10 years weren't copied or they were overcopied by the next company's data. So I used a macro that adds 10 rows every 5 rows, altered the code you provided to fit the new form of my archive and it worked just fine.
Thank you very much! If you hadn't helped me I would have been stuck there doing this manualy...
 
Upvote 0
I tried the code but there were a couple of problems which I fixed them in a way. The traspose was made only for the 5 first years. I thought that because the rows for each company were 5 and the years were 15, that the last 10 years weren't copied or they were overcopied by the next company's data. So I used a macro that adds 10 rows every 5 rows, altered the code you provided to fit the new form of my archive and it worked just fine.
Thank you very much! If you hadn't helped me I would have been stuck there doing this manualy...

I am glad you got it working, but I am a little confused. I copy/pasted the data you included in your original message into a worksheet (with the first header cell going to cell A1) and then, when I run the macro I posted, it outputs the exact table you showed that you wanted from that data... are you saying it is not doing that for you?
 
Upvote 0
Nataly92,

Welcome to the MrExcel forum.

With your raw data in worksheet Sheet1, the output you are looking for will be in a new worksheet Results.

Sample raw data in worksheet Sheet1:


Excel 2007
ABCDEFGHIJKLMNOP
1199819992000200120022003200420052006200720082009201020112012
2comp.1data1123456789101112131415
3comp.1data2161718192021222324252627282930
4comp.1data3313233343536373839404142434445
5comp.1data4464748495051525354555657585960
6comp.1data5616263646566676869707172737475
7comp.2data1767778798081828384858687888990
8comp.2data2919293949596979899100101102103104105
9comp.2data3106107108109110111112113114115116117118119120
10comp.2data4121122123124125126127128129130131132133134135
11comp.2data5136137138139140141142143144145146147148149150
12comp.3data1151152153154155156157158159160161162163164165
13comp.3data2166167168169170171172173174175176177178179180
14comp.3data3181182183184185186187188189190191192193194195
15comp.3data4196197198199200201202203204205206207208209210
16comp.3data5211212213214215216217218219220221222223224225
17
Sheet1


After the macro in a new worksheet Results:


Excel 2007
ABCDE
1data1data2data3data4data5
2116314661
3217324762
4318334863
5419344964
6520355065
7621365166
8722375267
9823385368
10924395469
111025405570
121126415671
131227425772
141328435873
151429445974
161530456075
177691106121136
187792107122137
197893108123138
207994109124139
218095110125140
228196111126141
238297112127142
248398113128143
258499114129144
2685100115130145
2786101116131146
2887102117132147
2988103118133148
3089104119134149
3190105120135150
32151166181196211
33152167182197212
34153168183198213
35154169184199214
36155170185200215
37156171186201216
38157172187202217
39158173188203218
40159174189204219
41160175190205220
42161176191206221
43162177192207222
44163178193208223
45164179194209224
46165180195210225
47
Results


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub ReorgData()
' hiker95, 10/19/2013
' http://www.mrexcel.com/forum/excel-questions/733822-traspose-each-row-every-5-rows-15-cells-into-one-column.html
Dim r As Long, lr As Long, lc As Long, nr As Long
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=Sheets("Sheet1")).Name = "Results"
With Sheets("Results")
  .UsedRange.ClearContents
  .Cells(1).Resize(, 5).Value = [{"data1","data2","data3","data4","data5"}]
End With
With Sheets("Sheet1")
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  For r = 2 To lr Step 5
    nr = Sheets("Results").Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    Sheets("Results").Range("A" & nr).Resize(lc - 1, 5).Value = Application.Transpose(.Range(.Cells(r, 2), .Cells(r + 4, lc)))
  Next r
End With
With Sheets("Results")
  .Columns.AutoFit
  .Activate
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgData macro.
 
Upvote 0
I am glad you got it working, but I am a little confused. I copy/pasted the data you included in your original message into a worksheet (with the first header cell going to cell A1) and then, when I run the macro I posted, it outputs the exact table you showed that you wanted from that data... are you saying it is not doing that for you?

Hi Rick,

Here is what I got with your code (in yellow):

comp.1data1
comp.1data2
comp.1data3
comp.1data4
comp.1data5
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
1
16
31
46
61
5
6
7
8
9
10
11
12
13
14
15
2
17
32
47
62
20
21
22
23
24
25
26
27
28
29
30
3
18
33
48
63
35
36
37
38
39
40
41
42
43
44
45
4
19
34
49
64
50
51
52
53
54
55
56
57
58
59
60
5
20
35
50
65
65
66
67
68
69
70
71
72
73
74
75
21
22
23
24
25
80
81
82
83
84
85
86
87
88
89
90
36
37
38
39
40
95
96
97
98
99
100
101
102
103
104
105
51
52
53
54
55
110
111
112
113
114
115
116
117
118
119
120
66
67
68
69
70
125
126
127
128
129
130
131
132
133
134
135
80
95
110
125
140
140
141
142
143
144
145
146
147
148
149
150
96
97
98
99
100
155
156
157
158
159
160
161
162
163
164
165
111
112
113
114
115
170
171
172
173
174
175
176
177
178
179
180
126
127
128
129
130
185
186
187
188
189
190
191
192
193
194
195
141
142
143
144
145
200
201
202
203
204
205
206
207
208
209
210
155
170
185
200
215
215
216
217
218
219
220
221
222
223
224
225
156
171
186
201
216
157
172
187
202
217
158
173
188
203
218
159
174
189
204
219
160
175
190
205
220
161
176
191
206
221
162
177
192
207
222
163
178
193
208
223
164
179
194
209
224
165
180
195
210
225

<tbody>
</tbody>


I did a small modification (in red).

Code:
Sub TransposeData_mk()
  Dim X As Long, R As Long
  R = 2
  With Sheets("Sheet2")
    .Range([COLOR="#FF0000"]"R1:V1"[/COLOR]).Value = WorksheetFunction.Transpose(Range("A2:A6").Value)
    For X = 2 To .Cells(Rows.Count, "A").End(xlUp).Row Step 5
        .Cells(R, "R").Resize(15, 5) = WorksheetFunction.Transpose(Cells(X, "B").Resize(5, 15).Value)
        R = R + [COLOR="#FF0000"]15[/COLOR]
    Next
  End With
End Sub

I think it's ok now.

Markmzz
 
Upvote 0
Hi Rick,

Here is what I got with your code (in yellow):

comp.1data1
comp.1data2
comp.1data3
comp.1data4
comp.1data5
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
1
16
31
46
61
5
6
7
8
9
10
11
12
13
14
15
2
17
32
47
62
20
21
22
23
24
25
26
27
28
29
30
3
18
33
48
63
35
36
37
38
39
40
41
42
43
44
45
4
19
34
49
64
50
51
52
53
54
55
56
57
58
59
60
5
20
35
50
65
65
66
67
68
69
70
71
72
73
74
75
21
22
23
24
25
80
81
82
83
84
85
86
87
88
89
90
36
37
38
39
40
95
96
97
98
99
100
101
102
103
104
105
51
52
53
54
55
110
111
112
113
114
115
116
117
118
119
120
66
67
68
69
70
125
126
127
128
129
130
131
132
133
134
135
80
95
110
125
140
140
141
142
143
144
145
146
147
148
149
150
96
97
98
99
100
155
156
157
158
159
160
161
162
163
164
165
111
112
113
114
115
170
171
172
173
174
175
176
177
178
179
180
126
127
128
129
130
185
186
187
188
189
190
191
192
193
194
195
141
142
143
144
145
200
201
202
203
204
205
206
207
208
209
210
155
170
185
200
215
215
216
217
218
219
220
221
222
223
224
225
156
171
186
201
216
157
172
187
202
217
158
173
188
203
218
159
174
189
204
219
160
175
190
205
220
161
176
191
206
221
162
177
192
207
222
163
178
193
208
223
164
179
194
209
224
165
180
195
210
225

<TBODY>
</TBODY>


I did a small modification (in red).

Code:
Sub TransposeData_mk()
  Dim X As Long, R As Long
  R = 2
  With Sheets("Sheet2")
    .Range([COLOR=#000000]"R1:V1"[/COLOR]).Value = WorksheetFunction.Transpose(Range("A2:A6").Value)
    For X = 2 To .Cells(Rows.Count, "A").End(xlUp).Row Step 5
        .Cells(R, [B][COLOR=#b22222]"R"[/COLOR][/B]).Resize(15, 5) = WorksheetFunction.Transpose(Cells(X, "B").Resize(5, 15).Value)
        R = R + [COLOR=#0000ff][B]15[/B][/COLOR]
    Next
  End With
End Sub
You also changed the text I highlighted in red as well. But why did you change the 5 to 15 (highlighted in blue)? That would not match what the OP posted she wanted originally. As for the yellow highlighted part.... isn't that what the OP indicated she wanted in her original message?
 
Upvote 0
You also changed the text I highlighted in red as well. But why did you change the 5 to 15 (highlighted in blue)? That would not match what the OP posted she wanted originally. As for the yellow highlighted part.... isn't that what the OP indicated she wanted in her original message?

1) Thanks. I forgot the "R" in Cells(R, "R").

2) Before, the data have 15 columns, then, after the use of the function TRANSPOSE, we have 15 rows. So you must to add 15 rows and not 5 rows for the next step. Look at the second table of the post #1.

Markmzz
 
Upvote 0
2) Before, the data have 15 columns, then, after the use of the function TRANSPOSE, we have 15 rows. So you must to add 15 rows and not 5 rows for the next step. Look at the second table of the post #1.

You are right... a mental lapse on my part (I only got about 3 hours sleep last night and it looks like that may be showing in my posts). Thanks for noting that for me.
 
Upvote 0
You are right... a mental lapse on my part (I only got about 3 hours sleep last night and it looks like that may be showing in my posts). Thanks for noting that for me.

You're welcome.

And sometimes, we all make mistakes, especially when we're tired.

Markmzz
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,462
Members
448,899
Latest member
maplemeadows

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