How to Transpose large data in Excel?

jbowers221

New Member
Joined
Feb 16, 2018
Messages
1
Hi all--
I need help on how to transpose a large set of data which contains: year, term, student id, and sports. It gets complicated because there are some students that played multiple sports each term.

Here's what the raw data looks like:
[TABLE="width: 500"]
<tbody>[TR]
[TD]YEAR_CODE
[/TD]
[TD]TERM_CODE
[/TD]
[TD]ID NUM
[/TD]
[TD]SPORTS
[/TD]
[/TR]
[TR]
[TD]2013-2014
[/TD]
[TD]20
[/TD]
[TD]31364
[/TD]
[TD]Tennis
[/TD]
[/TR]
[TR]
[TD]2014-2015
[/TD]
[TD]20
[/TD]
[TD]31364
[/TD]
[TD]Tennis
[/TD]
[/TR]
[TR]
[TD]2015-2016
[/TD]
[TD]20
[/TD]
[TD]31364
[/TD]
[TD]Tennis
[/TD]
[/TR]
[TR]
[TD]2013-2014
[/TD]
[TD]10
[/TD]
[TD]10235
[/TD]
[TD]Soccer
[/TD]
[/TR]
[TR]
[TD]2014-2015
[/TD]
[TD]10
[/TD]
[TD]10235
[/TD]
[TD]Soccer
[/TD]
[/TR]
[TR]
[TD]2013-2014
[/TD]
[TD]20
[/TD]
[TD]10235
[/TD]
[TD]Tracks
[/TD]
[/TR]
[TR]
[TD]2015-2016
[/TD]
[TD]10
[/TD]
[TD]11866
[/TD]
[TD]Basketball
[/TD]
[/TR]
</tbody>[/TABLE]
*term 10 = fall; 20 = spring
*year = academic year

I want the format to look like this:
[TABLE="width: 500"]
<tbody>[TR]
[TD]ID NUM
[/TD]
[TD="align: center"]2013-14
[/TD]
[TD="align: center"]2013-14
[/TD]
[TD="align: center"]2014-15
[/TD]
[TD="align: center"]2014-15
[/TD]
[TD="align: center"]2015-16
[/TD]
[TD="align: center"]2015-16
[/TD]
[/TR]
[TR]
[TD="align: center"][/TD]
[TD="align: center"]10
[/TD]
[TD="align: center"]20
[/TD]
[TD="align: center"]10
[/TD]
[TD="align: center"]20
[/TD]
[TD="align: center"]10
[/TD]
[TD="align: center"]20
[/TD]
[/TR]
[TR]
[TD]31364
[/TD]
[TD][/TD]
[TD]Tennis
[/TD]
[TD][/TD]
[TD]Tennis
[/TD]
[TD][/TD]
[TD]Tennis
[/TD]
[/TR]
[TR]
[TD]10235
[/TD]
[TD]Soccer
[/TD]
[TD]Tracks
[/TD]
[TD]Soccer
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]11866
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Basketball
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I tried the basic transpose method but it does not look like the one I have on the top. What kind of formula do I have to use for this?

Thanks in advance for your help!

jbowers
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG16Feb31
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] R [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        n = n + 2
        .Add Dn.Value, n
    [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]Next[/COLOR] Dn
 
 [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Offset(, 2)
    [COLOR="Navy"]If[/COLOR] Not Dic.Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        Dic.Add Dn.Value, Dn
    [COLOR="Navy"]Else[/COLOR]
     [COLOR="Navy"]Set[/COLOR] Dic(Dn.Value) = Union(Dic(Dn.Value), Dn)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
ReDim nray(1 To Dic.Count + 3, 1 To (.Count + 1) * 2)
nray(1, 1) = "ID NUM"
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
        nray(1, .Item(K)) = K: nray(1, .Item(K) + 1) = K
        nray(2, .Item(K)) = 10: nray(2, .Item(K) + 1) = 20
    [COLOR="Navy"]Next[/COLOR] K
c = 2
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] Dic.keys
c = c + 1
nray(c, 1) = K
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] Dic(K)
        num = IIf(R.Offset(, -1) = 20, 1, 0)
        nray(c, .Item(R.Offset(, -2).Value) + num) = R.Offset(, 1).Value
     [COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, UBound(nray, 2))
    .Value = nray
    .Borders.Weight = 2
    .Columns.AutoFit
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


This may help:-
To Save and Run Code:-
Copy code from Thread
In Your Data sheet , Click "Alt+F11",:- Vb Window appears.
From the VBWindow toolbar, Click "Insert" ,"Module":- New VBwindow appears .
Paste Code into this window.
Close Vbwindow.
On sheet Click "Developer tab", Click "Macro". Macro dialog box appears.
Select Macro (with same name) from List.
On the right of Dialog box Click "Run"
Sheet2 should now be updated.



Regards Mick
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

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