Macro to Organise some data, Transpose and grouping.

bushidowarrior

Board Regular
Joined
Jun 27, 2011
Messages
84
Office Version
  1. 2019
Platform
  1. Windows
Hello all.

I have the table located below, but I have to organsise the data in to the format of Fig 2.

Fig 1. the data the is carried over is Column D, G and H



From this (Fig 1)
FopqQBh.png



All the info below come from the above table, as you can see the data is grouped the number is column A.

So, group 1 has only 2 questions whereas group 3 has 3 questions.

I would like it so it can support up to 20 questions on 1 row. The above image should support at least 100 rows.



To this (Fig 2)
MIEIoLm.png


I think I have explained it well, but I am not sure.

Thank you all very much.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
Code:
Sub RearrangeData()
   Dim Ary As Variant, Oary As Variant
   Dim r As Long, i As Long, c As Long
   
   With Sheets("Sheet1")
      Ary = .Range("D2", .Range("D" & Rows.Count).End(xlUp).Offset(, 4)).value2
   End With
   ReDim Oary(1 To UBound(Ary), 1 To 40)
   
   c = 1:   r = r + 1
   Oary(r, 1) = Ary(1, 1): Oary(r, 2) = Ary(1, 5): Oary(r, 3) = Ary(1, 4)
   For i = 2 To UBound(Ary)
      If Ary(i, 1) = Oary(r, 1) Then
         c = c + 2
         Oary(r, c + 1) = Ary(i, 5): Oary(r, c + 2) = Ary(i, 4)
      Else
         r = r + 1: c = 1
         Oary(r, c) = Ary(i, 1): Oary(r, c + 1) = Ary(i, 5): Oary(r, c + 2) = Ary(i, 4)
      End If
   Next i
   Sheets("sheet2").Range("A2").Resize(r, 40).Value = Oary
End Sub
 
Upvote 0
Hello,

Fantastic... This is great!!!!!!!!!!!!!!!!!!!!!

Wow...Just a quick thing please... Can it carry over the points column.

I would really appreciate that!
 
Upvote 0
It already does. If I start with


Excel 2013/2016
DEFGH
211{1}{1}
311{2}{2}
421{3}{3}
521{4}{4}
631{5}{5}
731{6}{6}
831{7}{7}
941{8}{8}
Sheet1


I end up with


Excel 2013/2016
ABCDEFG
21{1}{1}1{2}{2}1
32{3}{3}1{4}{4}1
43{5}{5}1{6}{6}1{7}{7}1
54{8}{8}1
Sheet2
 
Upvote 0
Indeed, you are correct. My mistake.

THANK YOU VERY MUCH!!!!!!!!!!!!!!!!!

It already does. If I start with

Excel 2013/2016
DEFGH
11{1}{1}
11{2}{2}
21{3}{3}
21{4}{4}
31{5}{5}
31{6}{6}
31{7}{7}
41{8}{8}

<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

[TD="align: center"]6[/TD]

[TD="align: center"]7[/TD]

[TD="align: center"]8[/TD]

[TD="align: center"]9[/TD]

</tbody>
Sheet1



I end up with

Excel 2013/2016
ABCDEFG
{1}{1}{2}{2}
{3}{3}{4}{4}
{5}{5}{6}{6}{7}{7}
{8}{8}

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]2[/TD]
[TD="align: right"]1[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]2[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]3[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]1[/TD]

[TD="align: right"]1[/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]4[/TD]

[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet2
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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