Macro Help Required for Multiple Column to Single Column Conversion (With Header Info)

charpotro

New Member
Joined
Jul 23, 2014
Messages
3
Dear Experts,

I intend to re-arrange the data generated by system. I am using Office 2010

Current Format:

Mat No Cost1 Cost2 Cost3
A 5 3 2
B 6 9 3
C 7 6 2

To be Format:

Mat No
A 5 Cost1
B 6 Cost1
C 7 Cost1
A 3 Cost2
B 9 Cost2
C 6 Cost2
A 2 Cost3
B 3 Cost3
C 2 Cost3


Will be greatful if anybody can suggest the VB code for the rearrangement. The 'Mat No' header is optional, it may or may not appear in the 'to be' format - whichever is easier.

Regards
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Sorry, the formats in the original post may not be legible, revising it here:

Current Format:
[TABLE="width: 192"]
<COLGROUP><COL style="WIDTH: 48pt" span=4 width=64><TBODY>[TR]
[TD="class: xl65, width: 64, bgcolor: transparent"]Mat No[/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"]Cost1[/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"]Cost2[/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"]Cost3[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]A[/TD]
[TD="class: xl66, bgcolor: transparent"]5[/TD]
[TD="class: xl66, bgcolor: transparent"]3[/TD]
[TD="class: xl66, bgcolor: transparent"]2[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]B[/TD]
[TD="class: xl66, bgcolor: transparent"]6[/TD]
[TD="class: xl66, bgcolor: transparent"]9[/TD]
[TD="class: xl66, bgcolor: transparent"]3[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]C[/TD]
[TD="class: xl66, bgcolor: transparent"]7[/TD]
[TD="class: xl66, bgcolor: transparent"]6[/TD]
[TD="class: xl66, bgcolor: transparent"]2[/TD]
[/TR]
</TBODY>[/TABLE]


To be Format:

[TABLE="width: 144"]
<COLGROUP><COL style="WIDTH: 48pt" span=3 width=64><TBODY>[TR]
[TD="class: xl65, width: 64, bgcolor: transparent"]Mat No[/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"] [/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]A[/TD]
[TD="class: xl66, bgcolor: transparent"]5[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost1[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]B[/TD]
[TD="class: xl66, bgcolor: transparent"]6[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost1[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]C[/TD]
[TD="class: xl66, bgcolor: transparent"]7[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost1[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]A[/TD]
[TD="class: xl66, bgcolor: transparent"]3[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost2[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]B[/TD]
[TD="class: xl66, bgcolor: transparent"]9[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost2[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]C[/TD]
[TD="class: xl66, bgcolor: transparent"]6[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost2[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]A[/TD]
[TD="class: xl66, bgcolor: transparent"]2[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost3[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]B[/TD]
[TD="class: xl66, bgcolor: transparent"]3[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost3[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]C[/TD]
[TD="class: xl66, bgcolor: transparent"]2[/TD]
[TD="class: xl66, bgcolor: transparent"]Cost3[/TD]
[/TR]
</TBODY>[/TABLE]


Regards
 
Upvote 0
charpotro,

Welcome to the MrExcel forum.

The below macro will adjust for a varying number or rows, and, columns.


If the raw data is not in worksheet Sheet1, then, let me know the actual raw data worksheet name, and, I will adjust the macro.

If you want to have the results in a new worksheet, then let me known the new worksheet name.


Sample raw data on worksheet Sheet1:


Excel 2007
ABCDEFGHI
1Mat NoCost1Cost2Cost3
2A532
3B693
4C762
5
6
7
8
9
10
11
Sheet1


After the macro using two arrays in memory:


Excel 2007
ABCDEFGHI
1Mat NoCost1Cost2Cost3Mat No
2A532A5Cost1
3B693B6Cost1
4C762C7Cost1
5A3Cost2
6B9Cost2
7C6Cost2
8A2Cost3
9B3Cost3
10C2Cost3
11
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
2. Open your NEW 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
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 07/24/2014, ME793789
Dim a As Variant, o As Variant
Dim i As Long, j As Long, c As Long
Dim lr As Long, lc As Long, luc As Long, n As Long
With Sheets("Sheet1")
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, 1).End(xlToRight).Column
  luc = .Cells(1, Columns.Count).End(xlToLeft).Column
  If luc > lc Then .Columns(luc).Resize(, 3).ClearContents
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  n = Application.CountA(.Range(.Cells(2, 2), .Cells(lr, lc)))
  ReDim o(1 To n, 1 To 3)
  For c = 2 To lc Step 1
    For i = 2 To lr Step 1
      j = j + 1
      o(j, 1) = a(i, 1)
      o(j, 2) = a(i, c)
      o(j, 3) = a(1, c)
    Next i
  Next c
  .Cells(1, lc + 3).Value = "Mat No"
  .Cells(2, lc + 3).Resize(n, 3).Value = o
  .Columns(lc + 3).Resize(, 3).AutoFit
End With
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
Thanks hiker95, the macro was very useful... it is working now... again thanks for taking the time to help out...
 
Upvote 0
charpotro,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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