macro: change in value & transpose data

quercus

New Member
Joined
Feb 23, 2009
Messages
19
Hello All,

Just wondering whether anybody can help me with a macro question?

I have two columns of data, for example as follows:

<table border="0" cellpadding="0" cellspacing="0" width="161"><colgroup><col style="width:48pt" width="64"> <col style="mso-width-source:userset;mso-width-alt:1206;width:25pt" width="33"> <col style="width:48pt" width="64"> </colgroup><tbody><tr style="height:15.0pt" height="20"> <td colspan="2" style="height:15.0pt;mso-ignore:colspan; width:73pt" height="20" width="97">Module_code</td> <td style="width:48pt" width="64">Old_topic_code</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR321M</td> <td>
</td> <td>GR321</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR321M</td> <td>
</td> <td>GR322</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR321M</td> <td>
</td> <td>GR323</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR321M</td> <td>
</td> <td>GR320X</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR322M</td> <td>
</td> <td>GR324</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR322M</td> <td>
</td> <td>GR325</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR322M</td> <td>
</td> <td>GR326</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR320E</td> <td>
</td> <td>GR320X</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR001M</td> <td>
</td> <td>GR001</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR001M</td> <td>
</td> <td>GR002</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR001M</td> <td>
</td> <td>GR003</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR001M</td> <td>
</td> <td>GR004</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR001M</td> <td>
</td> <td>GR005</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR002M</td> <td>
</td> <td>GR006</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR002M</td> <td>
</td> <td>GR007</td> </tr> </tbody></table>

I would like to transpose the data, so that all the values in column Old_Topic_Code are detailed in the same row as the relevant module code, for example the output for GR321M and GR322M would be as follows:

<table border="0" cellpadding="0" cellspacing="0" width="353"><colgroup><col style="width:48pt" width="64"> <col style="mso-width-source:userset;mso-width-alt:1206;width:25pt" width="33"> <col style="width:48pt" span="4" width="64"> </colgroup><tbody><tr style="height:15.0pt" height="20"> <td class="xl65" colspan="2" style="height:15.0pt;mso-ignore: colspan;width:73pt" height="20" width="97">Module_code</td> <td class="xl65" colspan="2" style="mso-ignore:colspan;width:96pt" width="128">Old_topic_code</td> <td style="width:48pt" width="64">
</td> <td style="width:48pt" width="64">
</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR321M</td> <td>
</td> <td>GR321</td> <td>GR322</td> <td>GR323</td> <td>GR320X</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">GR322M</td> <td>
</td> <td>GR324</td> <td>GR325</td> <td>GR326</td> <td>
</td> </tr> </tbody></table><table border="0" cellpadding="0" cellspacing="0" height="40" width="349"><tbody><tr style="height:15.0pt" height="20"><td style="height:15.0pt" height="20">
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td></tr></tbody></table>This would then continue down the sheet, and each unique value in column Module_code would then detail the associated Old_topic_code values. As I have thousands of rows of data, the only way I can do this would be a macro, but just not sure how.

All assistance greatly appreciated!

Thanks
quercus
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Assuming that Column A and Column B contain the data, make sure the sheet containing the data is the active sheet and try the following macro...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] i [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 is available...", vbExclamation
        [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
    Application.ScreenUpdating = [color=darkblue]False[/color]
    
    [color=darkblue]With[/color] Range("A1:B" & LastRow)
        .Sort key1:=Range("A1"), Order1:=xlAscending, key2:=Range("B1"), Order2:=xlAscending, _
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTop[color=darkblue]To[/color]Bottom
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]For[/color] i = LastRow To 3 [color=darkblue]Step[/color] -1
        [color=darkblue]If[/color] Cells(i, "A").Value = Cells(i - 1, "A").Value [color=darkblue]Then[/color]
            Range(Cells(i, "B"), Cells(i, Columns.Count).End(xlToLeft)).Copy Cells(i - 1, "C")
            Rows(i).Delete
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] i
    
    Application.ScreenUpdating = [color=darkblue]True[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0

Forum statistics

Threads
1,224,604
Messages
6,179,857
Members
452,948
Latest member
UsmanAli786

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