Complex row to column help required!

BaffledOfBristol

New Member
Joined
Dec 24, 2013
Messages
13
My VBA/macro writing is a little rusty and I need to consolidate multiple rows into one row and then delete the old rows. My vehicle table has 1 row per bus per month. What I am trying to do is change the file so it contains only one row per bus with the monthly data in new columns.


The current columns are as follows (many rows per bus). The underlined columns are static/repeating for each monthly bus row. In bold are the columns that do change per bus per month :-


FLEET NO
REGISTRATION NO
VEHICLE TYPE
MODEL
COMPANY
ALLOCATED DEPOT
CLOSING ODOMETER
ODOMETER DAT
ODOMETER UNIT
FUEL USED IN PERIOD
MILES IN PERIOD
BSOG ELIGIBILITY



What I would like to do is have the data re-arranged as per the following columns with 1 row per bus:-


FLEET NO
REGISTRATION NO
VEHICLE TYPE
MODEL
COMPANY
ALLOCATED DEPOT
CLOSING ODOMETER1
ODOMETER DAT1
FUEL USED IN PERIOD1

MILES IN PERIOD1
CLOSING ODOMETER2
ODOMETER DAT2
FUEL USED IN PERIOD2

MILES IN PERIOD2

....
....
....
CLOSING ODOMETERn
ODOMETER DATn
FUEL USED IN PERIODn

MILES IN PERIODn

ODOMETER UNIT

BSOG ELIGIBILITY



It would be great if I could also order the repeating 'n' group of columns chronologically . Is this possible? The ODOMETER_DAT is in dd/mm/yyyy format.


The monthly data for the buses is not consistent i.e. the buses could have anywhere from one months data (i.e. 1 row) to a years worth (i.e. 12 rows). FLEETNO is the unique identifier for the bus.

Can anyone help please? Many thanks.
 
BaffledOfBristol,

Sample raw data in worksheet Sheet1 (I am using column M as a work area), before and after the macro:


Excel 2007
ABCDEFGHIJKLM
1FLEET NOREGISTRATION NOVEHICLE TYPEMODELCOMPANYALLOCATED DEPOTCLOSING ODOMETERODOMETER DATODOMETER UNITFUEL USED IN PERIODMILES IN PERIODBSOG ELIGIBILITY
212345A123ABCB7TLVOLVO B7TLBusCoBristol17298927-Apr-13M2031.82614TRUE
312345A123ABCB7TLVOLVO B7TLBusCoBristol17523525-May-13M1939.52246TRUE
412345A123ABCB7TLVOLVO B7TLBusCoBristol17570329-Jun-13M2656.73077TRUE
512345A123ABCB7TLVOLVO B7TLBusCoBristol17803027-Jul-13M2008.92327TRUE
612345A123ABCB7TLVOLVO B7TLBusCoBristol17998124-Aug-13M1684.81951TRUE
712345A123ABCB7TLVOLVO B7TLBusCoBristol17996928-Sep-13M2168.222511TRUE
812345A123ABCB7TLVOLVO B7TLBusCoBristol18254626-Oct-13M2105.72577TRUE
912345A123ABCB7TLVOLVO B7TLBusCoBristol16867729-Dec-12M2581.63041TRUE
1012345A123ABCB7TLVOLVO B7TLBusCoBristol16801126-Jan-13M1579.21860TRUE
1112345A123ABCB7TLVOLVO B7TLBusCoBristol17061523-Feb-13M2210.72604TRUE
1212345A123ABCB7TLVOLVO B7TLBusCoBristol17378030-Mar-13M26873165TRUE
13
Sheet1


After the macro in a new worksheet Results:


Excel 2007
ABCDEFGHIJKL
1FLEET NOREGISTRATION NOVEHICLE TYPEMODELCOMPANYALLOCATED DEPOTODOMETER UNITBSOG ELIGIBILITYCLOSING ODOMETER1ODOMETER DAT1FUEL USED IN PERIOD1MILES IN PERIOD1
212345A123ABCB7TLVOLVO B7TLBusCoBristolMTRUE16867729-Dec-122581.63041
3
Results


All the way out to column AZ:


Excel 2007
AOAPAQARASATAUAVAWAXAYAZ
1CLOSING ODOMETER9ODOMETER DAT9FUEL USED IN PERIOD9MILES IN PERIOD9CLOSING ODOMETER10ODOMETER DAT10FUEL USED IN PERIOD10MILES IN PERIOD10CLOSING ODOMETER11ODOMETER DAT11FUEL USED IN PERIOD11MILES IN PERIOD11
217998124-Aug-131684.8195117996928-Sep-132168.22251118254626-Oct-132105.72577
3
Results


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:
Option Explicit
Sub ReorgData()
' hiker95, 12/30/2013
' http://www.mrexcel.com/forum/excel-questions/746813-complex-row-column-help-required.html
Dim w1 As Worksheet, wR As Worksheet
Dim oa As Variant
Dim r As Long, rr As Long, lr As Long, lc As Long, n As Long, nr As Long, nc As Long, c As Long
Set w1 = Sheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Sheets("Results")
With Sheets("Results")
  .UsedRange.ClearContents
   w1.Range("A1:F1").Copy .Range("A1:F1")
   w1.Range("G1").Copy .Range("I1")
   .Range("I1") = .Range("I1") & "1"
   w1.Range("H1").Copy .Range("J1")
   .Range("J1") = .Range("J1") & "1"
   w1.Range("I1").Copy .Range("G1")
   w1.Range("J1").Copy .Range("K1")
   .Range("K1") = .Range("K1") & "1"
   w1.Range("K1").Copy .Range("L1")
   .Range("L1") = .Range("L1") & "1"
   w1.Range("L1").Copy .Range("H1")
End With
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  oa = .Range(.Cells(1, 1), .Cells(lr, lc + 1))
  With .Range("M2:M" & lr)
    .FormulaR1C1 = "=RC[-12]&RC[-11]"
    .Value = .Value
  End With
  .Range("A2:M" & lr).Sort key1:=.Range("M2"), order1:=1, key2:=.Range("H2"), order2:=1
  For r = 2 To lr
    n = Application.CountIf(w1.Columns(13), w1.Cells(r, 13).Value)
    nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    If n = 1 Then
      .Range("A" & r & ":F" & r).Copy wR.Range("A" & nr)
      .Range("G1").Copy wR.Range("I" & nr)
      .Range("H1").Copy wR.Range("J" & nr)
      .Range("I1").Copy wR.Range("G" & nr)
      .Range("J1").Copy wR.Range("K" & nr)
      .Range("K1").Copy wR.Range("L" & nr)
      .Range("L1").Copy wR.Range("H" & nr)
    ElseIf n > 1 Then
      .Range("A" & r & ":F" & r).Copy wR.Range("A" & nr)
      .Range("I" & r).Copy wR.Range("G" & nr)
      .Range("L" & r).Copy wR.Range("H" & nr)
      nc = 9
      For rr = r To r + n - 1
        .Range("G" & rr).Copy wR.Cells(nr, nc)
        .Range("H" & rr).Copy wR.Cells(nr, nc + 1)
        .Range("J" & rr).Copy wR.Cells(nr, nc + 2)
        .Range("K" & rr).Copy wR.Cells(nr, nc + 3)
        nc = nc + 4
      Next rr
    End If
    r = r + n - 1
  Next r
  w1.Range(w1.Cells(1, 1), w1.Cells(lr, lc + 1)) = oa
End With
With wR
  lc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
  n = 1
  For c = 13 To lc Step 4
    n = n + 1
    .Cells(1, c) = "CLOSING ODOMETER" & n
    .Cells(1, c + 1) = "ODOMETER DAT" & n
    .Cells(1, c + 2) = "FUEL USED IN PERIOD" & n
    .Cells(1, c + 3) = "MILES IN PERIOD" & n
  Next c
  .Range(.Cells(1, 13), .Cells(1, lc)).WrapText = True
  .Columns.AutoFit
  .Activate
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

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Many thanks hiker95,

I followed the instructions and ran the code as a module in the spreadsheet. I let it run for 40mins but it didn't complete. The egg timer (or its newest MS name) just kept spinning. The CPU on my laptop was also running constantly at 50% for the excel.exe. I had to kill the process in the end. I didn't think it should take 40mins to go through 75k rows. Could there have been a for loop or something that it got into?

Also in the results sheet, the only columns that were on row1 were the original column headings, not the new repeating 1-n columns.

Can you help please?
 
Upvote 0
hiker95,

Please ignore my previous post. I have tried it several more times with different macro security settings and it has now worked.

Many many thanks for all your help!
 
Upvote 0
Apologies again, I have gone through the data in the 'results' sheet and found an issue.

The 75k original rows are now down to 8436. Of these 360 have the column headings repeated in columns G-L instead of the data. I have gone through the original data for about 10 of these rows and can't see a pattern that might have triggered it. Can you help please?
 
Upvote 0
BaffledOfBristol,

I would have to see your workbook that is causing the problem.

You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
Hi Hiker95,

Many thanks for all your help. Unfortunately due to the nature of the data I cannot share it. However the errors are only 3.5% which I will take into consideration within the analysis.

thanks again,

BoB
 
Upvote 0
BaffledOfBristol,

Thanks for the feedback.

You are very welcome.

And, come back anytime.

Sorry I was not able to see your live data to be able to find a solution.
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,637
Members
449,461
Latest member
kokoanutt

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