VBA ReORG Data

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,180
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good Afternoon, I have a report that comes out of a certain system the same way all of the time. The sample below is a small snip-it of a much larger spreadsheet. What I am trying to do is to take a large file and reorg it on sheet 2 with the columns just like below. The 1st shot on the HTML maker shows how the data gets exported out. The 2nd shot on the HTML maker is what I am looking for. I am looking for code because I pull this report every week and want to make it user friendly. Tried a macro but it is a mess. Can someone please help me with some neater code!

Thanks in advance Stephen!

{ID,Name,Date,Subject,Score 1,Score 2,Score 3, Score 4, Score 5, Score 6, Score 7}


Book2
ABCDEFGHIJK
1DateNameIDSubjectScore 7Score 6Score 5Score 4Score 3Score 2Score 1
24/2/2011Sam83883Biology98987260726460
34/3/2011Sam72772English72727260815460
44/4/2011Steve16716Biology72727247899847
54/5/2011Steve28927Physics81818147147247
64/6/2011Keith38736English89898964147264
74/7/2011Keith49878English14141464568164
84/8/2011Al44332Physics14981481688954
94/9/2011Al56555English56725689981498
104/10/2011Mark87654Physics68726814721472
114/11/2011Mark12123Biology72815414725672
124/12/2011Fred12124Physics81899856816881
134/13/2011Fred12125English89147268896089
144/14/2011Peter12126English14147260146014
154/15/2011Peter12127Physics14568160144714
164/16/2011Peter12128English56685447564756
174/17/2011Peter12129Physics68569847686468
Sheet1



To this

Book2
ABCDEFGHIJK
1IDNameDateSubjectScore 1Score 2Score 3Score 4Score 5Score 6Score 7
283883Sam4/2/2011Biology60647260729898
372772Sam4/3/2011English60548160727272
416716Steve4/4/2011Biology47988947727272
528927Steve4/5/2011Physics47721447818181
638736Keith4/6/2011English64721464898989
749878Keith4/7/2011English64815664141414
844332Al4/8/2011Physics54896881149814
956555Al4/9/2011English98149889567256
1087654Mark4/10/2011Physics72147214687268
1112123Mark4/11/2011Biology72567214548172
1212124Fred4/12/2011Physics81688156988981
1312125Fred4/13/2011English89608968721489
1412126Peter4/14/2011English14601460721414
1512127Peter4/15/2011Physics14471460815614
1612128Peter4/16/2011English56475647546856
1712129Peter4/17/2011Physics68646847985668
Sheet2
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Stephen_IV,


Sample worksheets before the macro:


Excel Workbook
ABCDEFGHIJK
1DateNameIDSubjectScore 7Score 6Score 5Score 4Score 3Score 2Score 1
24/2/2011Sam83883Biology98987260726460
34/3/2011Sam72772English72727260815460
44/4/2011Steve16716Biology72727247899847
54/5/2011Steve28927Physics81818147147247
64/6/2011Keith38736English89898964147264
74/7/2011Keith49878English14141464568164
84/8/2011Al44332Physics14981481688954
94/9/2011Al56555English56725689981498
104/10/2011Mark87654Physics68726814721472
114/11/2011Mark12123Biology72815414725672
124/12/2011Fred12124Physics81899856816881
134/13/2011Fred12125English89147268896089
144/14/2011Peter12126English14147260146014
154/15/2011Peter12127Physics14568160144714
164/16/2011Peter12128English56685447564756
174/17/2011Peter12129Physics68569847686468
18
Sheet1





Excel Workbook
ABCDEFGHIJK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Sheet2





After the macro:


Excel Workbook
ABCDEFGHIJK
1IDNameDateSubjectScore 1Score 2Score 3Score 4Score 5Score 6Score 7
283883Sam4/2/2011Biology60647260729898
372772Sam4/3/2011English60548160727272
416716Steve4/4/2011Biology47988947727272
528927Steve4/5/2011Physics47721447818181
638736Keith4/6/2011English64721464898989
749878Keith4/7/2011English64815664141414
844332Al4/8/2011Physics54896881149814
956555Al4/9/2011English98149889567256
1087654Mark4/10/2011Physics72147214687268
1112123Mark4/11/2011Biology72567214548172
1212124Fred4/12/2011Physics81688156988981
1312125Fred4/13/2011English89608968721489
1412126Peter4/14/2011English14601460721414
1512127Peter4/15/2011Physics14471460815614
1612128Peter4/16/2011English56475647546856
1712129Peter4/17/2011Physics68646847985668
18
Sheet2





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
Option Base 1
Sub ReorgColumns()
' hiker95, 04/25/2011
' http://www.mrexcel.com/forum/showthread.php?t=545968
Dim w1 As Worksheet, w2 As Worksheet
Dim wary, a As Long, FC As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Sheet2!A1)") Then Worksheets.Add(After:=w1).Name = "Sheet2"
Set w2 = Worksheets("Sheet2")
w2.UsedRange.Clear
wary = Array("ID", "Name", "Date", "Subject", "Score 1", "Score 2", "Score 3", "Score 4", "Score 5", "Score 6", "Score 7")
For a = LBound(wary) To UBound(wary)
  FC = Application.Match(wary(a), w1.Rows(1), 0)
  w1.Columns(FC).Copy w2.Columns(a)
Next a
w2.Activate
Application.ScreenUpdating = True
End Sub


Then run the ReorgColumns macro.
 
Upvote 0
hiker95,

Absolutly Perfect! Thanks you again for your assistance!
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
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