Transpose Macro Needed

turner38

New Member
Joined
Jul 6, 2011
Messages
32
Hello,

I have a spreadsheet as shown below that has different columns of data organized vertically.
Sheet1
Excel Workbook
ABCDEF
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERMETRIC_IDACTUAL_VALUE
2201121ALPHAGBInput 11.000
3201121ALPHAGBInput 22.000
4201121ALPHAGBInput 33.000
5201121ALPHAGBInput 44.000
6201121ALPHAGBInput 55.000
7201121ALPHAGBInput 66.000
8201121ALPHAGBInput 77.000
9201121ALPHAGBInput 88.000
10201121ALPHAGBInput 99.000
...

Excel Workbook
ABCDEFGHIJKLM
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERInput 1Input 2Input 3Input 4Input 5Input 6Input 7Input 8Input 9
2201121ALPHAGB1.0002.0003.0004.0005.0006.0007.0008.0009.000
Excel 2007[/B] I am trying to find a macro that will transpose the above data into the format below. I can manually do it with the transpose function but I have about 20,000+ lines of data and it would take a long time to do. Weekly raw transposed

Excel 2007

I am trying to get the data into the format so I can put it in an Access table to run reports with.

Any Help is GREATLY Appreciated!!!!

John
 
Here is what I am starting with:
Excel Workbook
ABCDEF
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERMETRIC_IDACTUAL_VALUE
2201121ALPHAGBInput 11.000
3201121ALPHAGBInput 22.000
4201121ALPHAGBInput 33.000
5201121ALPHAGBInput 44.000
6201121ALPHAGBInput 55.000
7201121ALPHAGBInput 66.000
8201121ALPHAGBInput 77.000
9201121ALPHAGBInput 88.000
10201121ALPHAGBInput 99.000
11201121ALPHAGBInput 1010.000
12201121ALPHAGBInput 1111.000
13201221BETARDInput 111.000
14201221BETARDInput 210.000
15201221BETARDInput 39.000
16201221BETARDInput 48.000
17201221BETARDInput 57.000
18201221BETARDInput 66.000
19201221BETARDInput 75.000
20201221BETARDInput 84.000
21201221BETARDInput 93.000
22201221BETARDInput 102.000
23201221BETARDInput 111.000
Sheet1

Excel Workbook
ABCDEFGHIJKLM
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERInput 1Input 2Input 3Input 4Input 5Input 6Input 7Input 8Input 9
Excel 2007 And Here is how I am setting up my sheet 2 headings (Input 10 and Input 11 are there just can't fit them with the HTML Maker) Sheet2

#VALUE!
Excel 2007

The value 11 is missing for line 3 for RD- input 1.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
turner38,


Sample worksheets before the macro, and your raw data in Sheet1, already sorted and grouped per your screenshot:


Excel Workbook
ABCDEF
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERMETRIC_IDACTUAL_VALUE
2201121ALPHAGBInput 11
3201121ALPHAGBInput 22
4201121ALPHAGBInput 33
5201121ALPHAGBInput 44
6201121ALPHAGBInput 55
7201121ALPHAGBInput 66
8201121ALPHAGBInput 77
9201121ALPHAGBInput 88
10201121ALPHAGBInput 99
11201121ALPHAGBInput 1010
12201121ALPHAGBInput 1111
13201221BETARDInput 111
14201221BETARDInput 210
15201221BETARDInput 39
16201221BETARDInput 48
17201221BETARDInput 57
18201221BETARDInput 66
19201221BETARDInput 75
20201221BETARDInput 84
21201221BETARDInput 93
22201221BETARDInput 102
23201221BETARDInput 111
24
Sheet1





Excel Workbook
ABCDEFGHIJKLMNOP
1
2
3
4
Sheet2





After the macro:


Excel Workbook
ABCDEFGHIJKLMNO
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERInput 1Input 2Input 3Input 4Input 5Input 6Input 7Input 8Input 9Input 10Input 11
2201121ALPHAGB1234567891011
3201221BETARD1110987654321
4
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
Sub ReorgData()
' hiker95, 07/08/2011
' http://www.mrexcel.com/forum/showthread.php?t=562425
Dim LR As Long, SR As Long, ER As Long, a As Long, aa As Long, FC As Long
Application.ScreenUpdating = False
Worksheets("Sheet2").UsedRange.Clear
Worksheets("Sheet1").UsedRange.Copy Worksheets("Sheet2").Range("A1")
Worksheets("Sheet2").Activate
LR = Cells(Rows.Count, "A").End(xlUp).Row
With Range("G2:G" & LR)
  .FormulaR1C1 = "=RC[-6]&RC[-5]&RC[-4]&RC[-3]"
  .Value = .Value
End With
Columns("A:D").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("H1"), Unique:=True
LR = Cells(Rows.Count, "H").End(xlUp).Row
With Range("L2:L" & LR)
  .FormulaR1C1 = "=RC[-4]&RC[-3]&RC[-2]&RC[-1]"
  .Value = .Value
End With
Columns(5).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Columns(13), Unique:=True
LR = Cells(Rows.Count, "M").End(xlUp).Row
With Range("M1").Resize(, LR - 1)
  .Value = Application.Transpose(Range("M2:M" & LR))
  .Font.Bold = True
End With
Range("M2:M" & LR).ClearContents
LR = Cells(Rows.Count, "L").End(xlUp).Row
For a = 2 To LR Step 1
  SR = Application.Match(Range("L" & a), Columns(7), 0)
  ER = Application.Match(Range("L" & a), Columns(7), 1)
  For aa = SR To ER Step 1
    FC = 0
    On Error Resume Next
    FC = Application.Match(Range("E" & aa), Rows(1), 0)
    On Error GoTo 0
    If FC > 0 Then Cells(a, FC).Value = Range("F" & aa).Value
  Next aa
Next a
Columns("A:G").Delete
Columns(5).Delete
ActiveSheet.UsedRange.Columns.AutoFit
Application.ScreenUpdating = True
End Sub


Then run the ReorgData macro.
 
Upvote 0
turner38,

Thanks for the feedback.

You are very welcome.

Glad I could help.

Come back anytime.
 
Upvote 0
Hiker,

It worked perfectly a couple of times then it when I tried running it again it started getting Mismatch errors. Now, it runs with no errors but doesn't pull any of my values it just shows zeros instead. The header and data in columns A through E are correct, but the values in E through O are just zeros now.

Any idea what I did wrong? I have opened a new workbook and tried it several times with no luck.:confused:
 
Upvote 0
turner38,

Do any of the cells in your raw data contain formulas?

Can we get one of the workbooks where the macro is not working correctly (with sensative information changed).


See below in my Signature block: You can upload your workbook to Box Net, and provide us with a link to your workbook.
 
Upvote 0
Hiker,

I have uploaded my file to box.net. Here is the link:

http://www.box.net/shared/2kjtl3hngeoyyid08j3l

I have inlcuded about 2,000 lines of data in this file. I will be using this macro on about 20,000 lines if possible?

The input tab is where I paste my data (Columns A through F). I then run a macro to cleanse the data (Delete unwanted rows as well as rename the metric id. I then run the transpose macro and the re-configured data is on the Output tab, but it is still missing data values. Am I missing a step?

Also, You will notice I have a button on the Input tab labeled "Transpose Data". Ideally I would like to use this to run the macro instead of having to do ALT F8. Is this possible?

Is there any special rules I need to keep in mind in order for the macro to function properly?

Thanks for your help!
 
Upvote 0
turner38,


Thanks for the workbook.


Sample raw data before your Click Here First button is pressed:


Excel Workbook
ABCDEF
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERMETRIC_IDACTUAL_VALUE
2201122LU_FGPROMET1SP#-10.00
3201122LU_FGPROMET1SP#-20.00
4201122LU_FGPROMET1EE_1#-10.00
5201122LU_FGPROMET1EE_1#-20.00
6201122LU_FGPROMET1UH_1#-10.00
7201122LU_FGPROMET1UH_1#-20.00
8201122LU_FGPROMET1RLE (1)#_10.00
9201122LU_FGPROMET1RLE (1)#_20.00
10201122LU_FGPROMET1TC#_10.00
11201122LU_FGPROMET1TC#_20.00
12201122LU_FGPROMET1RE0.00
13201122LU_FGPROMET2SP#-124,830.00
14201122LU_FGPROMET2SP#-224,259.00
15201122LU_FGPROMET2EE_1#-124,259.00
16201122LU_FGPROMET2EE_1#-2104,000.00
17201122LU_FGPROMET2UH_1#-124,259.00
18201122LU_FGPROMET2UH_1#-2159.00
19201122LU_FGPROMET2RLE (1)#_166.00
20201122LU_FGPROMET2RLE (1)#_266.00
21201122LU_FGPROMET2TC#_1208.00
22201122LU_FGPROMET2TC#_266.00
23201122LU_FGPROMET2RE10,510.00
24201122LU_FGPROMET3-PROMET1SP#-10.00
25201122LU_FGPROMET3-PROMET1SP#-20.00
26201122LU_FGPROMET3-PROMET1EE_1#-10.00
27201122LU_FGPROMET3-PROMET1EE_1#-20.00
28201122LU_FGPROMET3-PROMET1UH_1#-10.00
29201122LU_FGPROMET3-PROMET1UH_1#-20.00
30201122LU_FGPROMET3-PROMET1RLE (1)#_10.00
31201122LU_FGPROMET3-PROMET1RLE (1)#_20.00
32201122LU_FGPROMET3-PROMET1TC#_10.00
33201122LU_FGPROMET3-PROMET1TC#_20.00
34201122LU_FGPROMET3-PROMET1RE0.00
35201122LU_FGPROMET3SP#-124830.000
36201122LU_FGPROMET3SP#-224259.000
37201122LU_FGPROMET3EE_1#-124259.000
38201122LU_FGPROMET3EE_1#-2104000.000
39201122LU_FGPROMET3UH_1#-124259.000
40201122LU_FGPROMET3UH_1#-2159.000
41201122LU_FGPROMET3RLE (1)#_166.000
42201122LU_FGPROMET3RLE (1)#_266.000
43201122LU_FGPROMET3TC#_1208.000
44201122LU_FGPROMET3TC#_266.000
45201122LU_FGPROMET3RE10510.000
Input





Sample raw data after your Click Here First button is pressed:


Excel Workbook
ABCDEF
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERMETRIC_IDACTUAL_VALUE
2201122LU_FGPROMET1Sp 10.00
3201122LU_FGPROMET1Sp 20.00
4201122LU_FGPROMET1E 10.00
5201122LU_FGPROMET1E 20.00
6201122LU_FGPROMET1UH 10.00
7201122LU_FGPROMET1UH 20.00
8201122LU_FGPROMET1RL 10.00
9201122LU_FGPROMET1RL 20.00
10201122LU_FGPROMET1TC 10.00
11201122LU_FGPROMET1TC 20.00
12201122LU_FGPROMET1RE0.00
13201122LU_FGPROMET2Sp 124,830.00
14201122LU_FGPROMET2Sp 224,259.00
15201122LU_FGPROMET2E 124,259.00
16201122LU_FGPROMET2E 2104,000.00
17201122LU_FGPROMET2UH 124,259.00
18201122LU_FGPROMET2UH 2159.00
19201122LU_FGPROMET2RL 166.00
20201122LU_FGPROMET2RL 266.00
21201122LU_FGPROMET2TC 1208.00
22201122LU_FGPROMET2TC 266.00
23201122LU_FGPROMET2RE10,510.00
24201122LU_FGPROMET3-PROMET1Sp 10.00
25201122LU_FGPROMET3-PROMET1Sp 20.00
26201122LU_FGPROMET3-PROMET1E 10.00
27201122LU_FGPROMET3-PROMET1E 20.00
28201122LU_FGPROMET3-PROMET1UH 10.00
29201122LU_FGPROMET3-PROMET1UH 20.00
30201122LU_FGPROMET3-PROMET1RL 10.00
31201122LU_FGPROMET3-PROMET1RL 20.00
32201122LU_FGPROMET3-PROMET1TC 10.00
33201122LU_FGPROMET3-PROMET1TC 20.00
34201122LU_FGPROMET3-PROMET1RE0.00
35201122LU_FGPROMET3Sp 124830.000
36201122LU_FGPROMET3Sp 224259.000
37201122LU_FGPROMET3E 124259.000
38201122LU_FGPROMET3E 2104000.000
39201122LU_FGPROMET3UH 124259.000
40201122LU_FGPROMET3UH 2159.000
41201122LU_FGPROMET3RL 166.000
42201122LU_FGPROMET3RL 266.000
43201122LU_FGPROMET3TC 1208.000
44201122LU_FGPROMET3TC 266.000
45201122LU_FGPROMET3RE10510.000
Input





After the macro in worksheet Output:


Excel Workbook
ABCDEFGHIJKLMNO
1YEAR_NUMBERWEEK_NUMBERLOCATION_IDPRODUCTION_METHOD_NUMBERSp 1Sp 2E 1E 2UH 1UH 2RL 1RL 2TC 1TC 2RE
2201122LU_FGPROMET10.000.000.000.000.000.000.000.000.000.000.00
3201122LU_FGPROMET224,830.0024,259.0024,259.00104,000.0024,259.00159.0066.0066.00208.0066.0010,510.00
4201122LU_FGPROMET3-PROMET10.000.000.000.000.000.000.000.000.000.000.00
5201122LU_FGPROMET324,830.0024,259.0024,259.00104,000.0024,259.00159.0066.0066.00208.0066.0010,510.00
Output





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 ReorgDataV2()
' hiker95, 07/09/2011
' http://www.mrexcel.com/forum/showthread.php?t=562425
Dim LR As Long, LR2 As Long, SR As Long, ER As Long, a As Long, aa As Long, FC As Long, LC As Long
Application.ScreenUpdating = False
Worksheets("Output").UsedRange.Clear
Worksheets("Input").UsedRange.Copy Worksheets("Output").Range("A1")
Worksheets("Output").Activate
LR = Cells(Rows.count, "A").End(xlUp).Row
With Range("G2:G" & LR)
  .FormulaR1C1 = "=RC[-6]&RC[-5]&RC[-4]&RC[-3]"
  .Value = .Value
End With
Columns("A:D").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("H1"), Unique:=True
LR = Cells(Rows.count, "H").End(xlUp).Row
With Range("L2:L" & LR)
  .FormulaR1C1 = "=RC[-4]&RC[-3]&RC[-2]&RC[-1]"
  .Value = .Value
End With
LR = Cells(Rows.count, "L").End(xlUp).Row
With Range("M2:M" & LR)
  .FormulaR1C1 = "=MATCH(RC[-1],C[-6],0)"
  .Value = .Value
End With
With Range("N2:N" & LR - 1)
  .FormulaR1C1 = "=R[1]C[-1]-1"
  .Value = .Value
End With
LR2 = Cells(Rows.count, "G").End(xlUp).Row
Range("N" & LR) = LR2
Columns(5).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Columns(15), Unique:=True
LR = Cells(Rows.count, "O").End(xlUp).Row
With Range("O1").Resize(, LR - 1)
  .Value = Application.Transpose(Range("O2:O" & LR))
  .Font.Bold = True
End With
Range("O2:O" & LR).ClearContents
LR = Cells(Rows.count, "L").End(xlUp).Row
For a = 2 To LR Step 1
  SR = Range("M" & a)
  ER = Range("N" & a)
  For aa = SR To ER Step 1
    FC = 0
    On Error Resume Next
    FC = Application.Match(Range("E" & aa), Rows(1), 0)
    On Error GoTo 0
    If FC > 0 Then Cells(a, FC).Value = Range("F" & aa).Value
  Next aa
Next a
Columns("A:G").Delete
Columns("E:G").Delete
LR = Cells(Rows.count, "A").End(xlUp).Row
LC = Cells(1, Columns.count).End(xlToLeft).Column
Range(Cells(2, 5), Cells(LR, LC)).NumberFormat = "#,##0.00"
ActiveSheet.UsedRange.Columns.AutoFit
Application.ScreenUpdating = 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 ReorgDataV2 macro.


You will have to assign the macro to the Transpose Data button.
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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