Move selected column data to new rows

colaboy

New Member
Joined
Apr 26, 2011
Messages
3
I have the following sheet where columns H-M should be moved from columns to new rows to be able to use the data effectively in pivot tables.
Excel Workbook
ABCDEFGHIJKLM
1DateVarietyGHGhsubFarmStemsLQ35cm40cm50cm60cm70cm80cm
201/08/2011SAMBA PA TI3Lower8039001070
301/08/2011UPPER GREEN0Lower30150030
401/08/2011HIGH AND EXOTIC16Lower00
501/08/2011MARIE CLAIRE31Lower50300050
601/08/2011MARIE CLAIRE32Lower140645030306020
701/08/2011MARIE CLAIRE33Lower24011200150403020
801/08/2011GOOD TIMES3Lower00
901/08/2011BURGUNDY3Lower00
1001/08/2011BURGUNDY16Lower17010500101302010
1101/08/2011SATISFACTION16Lower2011001010
Gradeout
Excel 2010

I want to create something like this:
Excel Workbook
ABCDEFGH
1DateVarietyGHGhsubFarmStemsLQLength
201/08/2011SAMBA PA TI3Lower035
301/08/2011SAMBA PA TI3Lower1040040
401/08/2011SAMBA PA TI3Lower70350050
501/08/2011SAMBA PA TI3Lower060
601/08/2011SAMBA PA TI3Lower070
701/08/2011SAMBA PA TI3Lower080
801/08/2011UPPER GREEN0Lower035
901/08/2011UPPER GREEN0Lower040
1001/08/2011UPPER GREEN0Lower30150050
1101/08/2011UPPER GREEN0Lower060
1201/08/2011UPPER GREEN0Lower070
1301/08/2011UPPER GREEN0Lower080
1401/08/2011HIGH AND EXOTIC16Lower035
1501/08/2011HIGH AND EXOTIC16Lower040
1601/08/2011HIGH AND EXOTIC16Lower050
1701/08/2011HIGH AND EXOTIC16Lower060
1801/08/2011HIGH AND EXOTIC16Lower070
1901/08/2011HIGH AND EXOTIC16Lower080
2001/08/2011MARIE CLAIRE31Lower035
2101/08/2011MARIE CLAIRE31Lower040
2201/08/2011MARIE CLAIRE31Lower050
2301/08/2011MARIE CLAIRE31Lower50300060
2401/08/2011MARIE CLAIRE31Lower070
2501/08/2011MARIE CLAIRE31Lower080
Gradeout New
Excel 2010

I am sure this has been explained before but I cannot find anything suitable by searching.

I have data for several years and an option for removing zero value rows (in column "Stems") would help reduce the size of the sheet.

Is there a simple way to make the transformation or does VBA have to be used?

Any help in getting started is most welcome (I am stuck).

PS. I would probably be better off working in Access but I am not very familiar with it nor are the clerks inputting the data.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
colaboy,


Welcome to the MrExcel forum.


Sample raw data on worksheet Gradeout:


Excel Workbook
ABCDEFGHIJKLM
1DateVarietyGHGhsubFarmStemsLQ35cm40cm50cm60cm70cm80cm
21/8/2011SAMBA PA TI3Lower8039001070
31/8/2011UPPER GREEN0Lower30150030
41/8/2011HIGH AND EXOTIC16Lower00
51/8/2011MARIE CLAIRE31Lower50300050
61/8/2011MARIE CLAIRE32Lower140645030306020
71/8/2011MARIE CLAIRE33Lower24011200150403020
81/8/2011GOOD TIMES3Lower00
91/8/2011BURGUNDY3Lower00
101/8/2011BURGUNDY16Lower17010500101302010
111/8/2011SATISFACTION16Lower2011001010
12
Gradeout





After the macro in a new or cleared worksheet Gradeout New:


Excel Workbook
ABCDEFGH
1DateVarietyGHGhsubFarmStemsLQLength
21/8/2011SAMBA PA TI3Lower035
31/8/2011SAMBA PA TI3Lower1040040
41/8/2011SAMBA PA TI3Lower70350050
51/8/2011SAMBA PA TI3Lower060
61/8/2011SAMBA PA TI3Lower070
71/8/2011SAMBA PA TI3Lower080
81/8/2011UPPER GREEN0Lower035
91/8/2011UPPER GREEN0Lower040
101/8/2011UPPER GREEN0Lower30150050
111/8/2011UPPER GREEN0Lower060
121/8/2011UPPER GREEN0Lower070
131/8/2011UPPER GREEN0Lower080
141/8/2011HIGH AND EXOTIC16Lower035
151/8/2011HIGH AND EXOTIC16Lower040
161/8/2011HIGH AND EXOTIC16Lower050
171/8/2011HIGH AND EXOTIC16Lower060
181/8/2011HIGH AND EXOTIC16Lower070
191/8/2011HIGH AND EXOTIC16Lower080
201/8/2011MARIE CLAIRE31Lower035
211/8/2011MARIE CLAIRE31Lower040
221/8/2011MARIE CLAIRE31Lower050
231/8/2011MARIE CLAIRE31Lower50300060
241/8/2011MARIE CLAIRE31Lower070
251/8/2011MARIE CLAIRE31Lower080
261/8/2011MARIE CLAIRE32Lower30105035
271/8/2011MARIE CLAIRE32Lower30120040
281/8/2011MARIE CLAIRE32Lower60300050
291/8/2011MARIE CLAIRE32Lower20120060
301/8/2011MARIE CLAIRE32Lower070
311/8/2011MARIE CLAIRE32Lower080
321/8/2011MARIE CLAIRE33Lower035
331/8/2011MARIE CLAIRE33Lower150600040
341/8/2011MARIE CLAIRE33Lower40200050
351/8/2011MARIE CLAIRE33Lower30180060
361/8/2011MARIE CLAIRE33Lower20140070
371/8/2011MARIE CLAIRE33Lower080
381/8/2011GOOD TIMES3Lower035
391/8/2011GOOD TIMES3Lower040
401/8/2011GOOD TIMES3Lower050
411/8/2011GOOD TIMES3Lower060
421/8/2011GOOD TIMES3Lower070
431/8/2011GOOD TIMES3Lower080
441/8/2011BURGUNDY3Lower035
451/8/2011BURGUNDY3Lower040
461/8/2011BURGUNDY3Lower050
471/8/2011BURGUNDY3Lower060
481/8/2011BURGUNDY3Lower070
491/8/2011BURGUNDY3Lower080
501/8/2011BURGUNDY16Lower035
511/8/2011BURGUNDY16Lower040
521/8/2011BURGUNDY16Lower1050050
531/8/2011BURGUNDY16Lower130780060
541/8/2011BURGUNDY16Lower20140070
551/8/2011BURGUNDY16Lower1080080
561/8/2011SATISFACTION16Lower035
571/8/2011SATISFACTION16Lower040
581/8/2011SATISFACTION16Lower1050050
591/8/2011SATISFACTION16Lower1060060
601/8/2011SATISFACTION16Lower070
611/8/2011SATISFACTION16Lower080
62
Gradeout New





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 ReorgData()
' hiker95, 08/21/2011
' http://www.mrexcel.com/forum/showthread.php?t=573337
Dim wG As Worksheet, wN As Worksheet
Dim LR As Long, LC As Long, r As Long, c As Long, n As Long, NR As Long
Dim CM(), nn As Long
Application.ScreenUpdating = False
Set wG = Worksheets("Gradeout")
LR = wG.Cells(Rows.Count, 1).End(xlUp).Row
LC = wG.Cells(1, Columns.Count).End(xlToLeft).Column
ReDim CM(1 To LC - 8 + 1)
n = 0
For c = 8 To LC Step 1
  n = n + 1
  CM(n) = Replace(wG.Cells(1, c), "cm", "")
Next c
If Not Evaluate("ISREF('Gradeout New'!A1)") Then Worksheets.Add(After:=wG).Name = "Gradeout New"
Set wN = Worksheets("Gradeout New")
wN.UsedRange.Clear
wG.Range("A1:G1").Copy wN.Range("A1:G1")
With wN.Range("H1")
  .Value = "Length"
  .Font.Bold = True
End With
n = LC - 8 + 1
For r = 2 To LR Step 1
  NR = wN.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
  wN.Range("A" & NR).Resize(n, 5).Value = wG.Range("A" & r).Resize(, 5).Value
  wN.Range("H" & NR).Resize(UBound(CM)).Value = Application.Transpose(CM)
  nn = NR
  For c = 8 To LC Step 1
    If wG.Cells(r, c) = "" Then         'And sw = 0 Then
      wN.Cells(nn, 7) = 0
    Else
      wN.Cells(nn, 6).Value = wG.Cells(r, c).Value
      wN.Cells(nn, 7).Value = wG.Cells(r, c).Value * CM(c - 7)
    End If
    nn = nn + 1
  Next c
Next r
Erase CM
wN.UsedRange.Columns.AutoFit
wN.Activate
Application.ScreenUpdating = True
End Sub


Then run the ReorgData macro.
 
Upvote 0
It works beautifully! (not fast but a lot faster than copy/paste)

Thanks so much hiker95!

I hope to be able to decipher the code soon ;-)
 
Upvote 0
colaboy,

You are very welcome.

Glad I could help.

Thanks for the feedback.


It works beautifully! (not fast but a lot faster than copy/paste)

Does worksheet Gradeout only have data in columns A thru M?

If so, let me try using arrays (which I am learning) that should really speed things up.
 
Upvote 0
colaboy,


I just tested the old and the new macro with 10,801 rows of raw data.

The old macro took 5 seconds, the new macro (with arrays working in memory) took 0.9 seconds.


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).


Code:
Option Explicit
Option Base 1
Sub ReorgDataV2()
' hiker95, 08/22/2011
' http://www.mrexcel.com/forum/showthread.php?t=573337
Dim wG As Worksheet, wN As Worksheet
Dim LR As Long, LC As Long, r As Long, c As Long, nr As Long, nn As Long
Dim CM(), G(), N()
Application.ScreenUpdating = False
Set wG = Worksheets("Gradeout")
LR = wG.Cells(Rows.Count, 1).End(xlUp).Row
LC = wG.Cells(1, Columns.Count).End(xlToLeft).Column
G = wG.Range("A1:M" & LR)
ReDim CM(1 To 6)
nn = 0
For c = 8 To 13 Step 1
  nn = nn + 1
  CM(nn) = Replace(G(1, c), "cm", "")
Next c
ReDim N(1 To ((UBound(G) - 1) * 6) + 1, 1 To 8)
For c = 1 To 7
  N(1, c) = G(1, c)
Next c
N(1, 8) = "Length"
nr = 2
For r = 2 To UBound(G)
  For nn = nr To nr + 5
    For c = 1 To 5
      N(nn, c) = G(r, c)
    Next c
  Next nn
  c = 0
  For nn = nr To nr + 5
    c = c + 1
    N(nn, 8) = CM(c)
  Next nn
  nn = nr
  For c = 8 To 13
    If G(r, c) = "" Then
      N(nn, 7) = 0
    Else
      N(nn, 6) = G(r, c)
      N(nn, 7) = G(r, c) * CM(c - 7)
    End If
    nn = nn + 1
  Next c
  nr = nr + 6
Next r
If Not Evaluate("ISREF('Gradeout New'!A1)") Then Worksheets.Add(After:=wG).Name = "Gradeout New"
Set wN = Worksheets("Gradeout New")
wN.UsedRange.Clear
wN.Range("A1").Resize(UBound(N), 8) = N
wN.UsedRange.Columns.AutoFit
wN.Activate
Erase CM
Erase G
Erase N
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.
 
Upvote 0
Excellent hiker95!

The first version took more than 5 minutes to process 3152 rows of data on my system. (i7, Q740 @ 1.73GHz, 6GB RAM, 64 bit, 32-bit Office Pro)

V2 takes about one second!!!

Thanks again for coming to my rescue.

What could be the reason for the slowness of V1 on my system? Excel goes do non-responding "white screen" during running V1 while CPU stays at 1% for excel.exe.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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