Convert columns into duplication of rows

rcs

New Member
Joined
Jan 15, 2013
Messages
3
Dear all,

I have an example of a simple version of a table that I need to convert to different table structure.

Can anybody please help me with this.:confused:

I need to convert the following table...
[TABLE="class: grid, width: 200, align: left"]
<TBODY>[TR]
[TD]Dep
[/TD]
[TD]Type
[/TD]
[TD]2010
[/TD]
[TD]2011
[/TD]
[TD]2012
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]X
[/TD]
[TD][/TD]
[TD]1
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]Y
[/TD]
[TD]3
[/TD]
[TD]4
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]X
[/TD]
[TD]6
[/TD]
[TD]7
[/TD]
[TD]8
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]Y
[/TD]
[TD]9
[/TD]
[TD]10
[/TD]
[TD]11
[/TD]
[/TR]
</TBODY>[/TABLE]








into the following structure.
[TABLE="class: grid, width: 300, align: left"]
<TBODY>[TR]
[TD]Dep
[/TD]
[TD]Type
[/TD]
[TD]Year
[/TD]
[TD]Val
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]X
[/TD]
[TD]2010
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]X
[/TD]
[TD]2011
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]X
[/TD]
[TD]2012
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]Y
[/TD]
[TD]2010
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]Y
[/TD]
[TD]2011
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]Y
[/TD]
[TD]2012
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]X
[/TD]
[TD]2010
[/TD]
[TD]6
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]X
[/TD]
[TD]2011
[/TD]
[TD]7
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]X
[/TD]
[TD]2012
[/TD]
[TD]8
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]Y
[/TD]
[TD]2010
[/TD]
[TD]9
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]Y
[/TD]
[TD]2011
[/TD]
[TD]10
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]Y
[/TD]
[TD]2012
[/TD]
[TD]11
[/TD]
[/TR]
</TBODY>[/TABLE]





















Thank you in advance.
 
rcs,

Welcome to the MrExcel forum.


Sample raw data:


Excel Workbook
ABCDEFGHIJK
1DepType201020112012
2AX12
3AY345
4BX678
5BY91011
6
7
8
9
10
11
12
13
14
Sheet1





After the macro:


Excel Workbook
ABCDEFGHIJK
1DepType201020112012DepTypeYearVal
2AX12AX2010
3AY345AX20111
4BX678AX20122
5BY91011AY20103
6AY20114
7AY20125
8BX20106
9BX20117
10BX20128
11BY20109
12BY201110
13BY201211
14
Sheet1





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, by highlighting the code and pressing the keys CTRL + C
2. Open your 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 by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 01/15/2013
' http://www.mrexcel.com/forum/excel-questions/679465-convert-columns-into-duplication-rows.html
Dim i As Variant, o As Variant, n As Long
Dim r As Long, rr As Long, lr As Long, c As Long, lc As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
i = Range(Cells(1, 1), Cells(lr, lc))
n = ((lc - 2) * (lr - 1)) + 1
ReDim o(1 To n, 1 To 4)
rr = 1
o(rr, 1) = "Dep"
o(rr, 2) = "Type"
o(rr, 3) = "Year"
o(rr, 4) = "Val"
For r = 2 To UBound(i, 1)
  For c = 3 To lc
    rr = rr + 1
    o(rr, 1) = i(r, 1)
    o(rr, 2) = i(r, 2)
    o(rr, 3) = i(1, c)
    o(rr, 4) = i(r, c)
  Next c
Next r
Cells(1, lc + 3).Resize(UBound(o, 1), UBound(o, 2)) = o
Cells(1, lc + 3).Resize(, UBound(o, 2)).Font.Bold = True
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
rcs,

The above macro should also run correctly for more years in row 1 going to the right.
 
Upvote 0
Hi hiker95,

Great and thanks for your prompt reply. It looks good and I will have a look and try to run it on my data.

Kind Regards,
rcs
 
Upvote 0
rcs,

Thanks for the feedback.

You are very welcome. Glad I could help.

Come back anytime.
 
Upvote 0
hiker95,

Thanks a lot. Worked like a charm.

Performed well on 3000 rows and around 50 columns.
The result is a table with around 180.000 rows.

Thank you. :)

KR,
rcs
 
Upvote 0
rcs,

You are very welcome. Glad I could help.

Thanks for the feedback.

Come back anytime.
 
Upvote 0

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