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:
YEAR_CODE
TERM_CODE
ID NUM
SPORTS
2013-2014
20
31364
Tennis
2014-2015
20
31364
Tennis
2015-2016
20
31364
Tennis
2013-2014
10
10235
Soccer
2014-2015
10
10235
Soccer
2013-2014
20
10235
Tracks
2015-2016
10
11866
Basketball

<tbody>
</tbody>
*term 10 = fall; 20 = spring
*year = academic year

I want the format to look like this:
ID NUM
2013-14
2013-14
2014-15
2014-15
2015-16
2015-16
10
20
10
20
10
20
31364
Tennis
Tennis
Tennis
10235
Soccer
Tracks
Soccer
11866
Basketball

<tbody>
</tbody>

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
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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
 

Watch MrExcel Video

Forum statistics

Threads
1,090,109
Messages
5,412,473
Members
403,428
Latest member
ldmcd

This Week's Hot Topics

Top