Macro to flatten certain columns of a table into individual rows

SushantJain

New Member
Joined
Dec 9, 2014
Messages
2
Hi Guys,

I am new to macros and i have my current data in Excel 2007 in this format below:

vNyyi+


But i want to converge Electricity use, Gas use and Oil use under a common column names as energy use, as below:

wEoL0+


Can you please help me with the Macro/ VBA code required to automate this task?


Cheers
Sushant:biggrin:
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hello,

Have assumed 'Supplier' in in Cell A1 and have assumed you want the data in the same place. Have taken your request as literal.

Code:
Sub ENERGY()
    Columns("D:F").Insert
    Range("D1").Value = "Energy Type"
    Range("E1").Value = "Value"
    Range("F1").Value = "Production Share"
    For MY_ROWS = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
        Range(Cells(MY_ROWS + 1, 1), Cells(MY_ROWS + 2, 1)).EntireRow.Insert
        Range("A" & MY_ROWS & ":C" & MY_ROWS).Copy
        Range(Cells(MY_ROWS + 1, 1), Cells(MY_ROWS + 2, 1)).PasteSpecial (xlPasteValues)
        Range(Cells(MY_ROWS, 7), Cells(MY_ROWS, 9)).Copy
        Cells(MY_ROWS, 5).PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
            , Transpose:=True

        Range("G1:I1").Copy
        Cells(MY_ROWS, 4).PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
            , Transpose:=True
        Cells(MY_ROWS, 10).Copy
        Range(Cells(MY_ROWS, 6), Cells(MY_ROWS + 2, 6)).PasteSpecial (xlPasteValues)
    Next MY_ROWS
    Columns("G:J").Delete
End Sub

Does this work as expected?
 
Upvote 0
SushantJain,

You did not say where you wanted the results to be written to?

Sample raw data in the active worksheet:


Excel 2007
ABCDEFGHIJKLMNO
1SupplierSupplier TypePeriodElectricity use (kWh)Gas use (m3)Oild use (Liters)Production Share (%)
2ABCDisribution EquipmentJan-1375,65510020040%
3
4
5
Sheet1


After the macro (using two arrays in memory - should be pretty fast) in the same worksheet:


Excel 2007
AGHIJKLMNO
1SupplierProduction Share (%)SupplierSupplier TypePeriodEnergy TypeValueProduction Share
2ABC40%ABCDisribution EquipmentJan-13Electricity use (kWh)75,65540%
3ABCDisribution EquipmentJan-13Gas use (m3)10040%
4ABCDisribution EquipmentJan-13Oild use (Liters)20040%
5
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, 12/11/2014, ME823036
Dim a As Variant, o As Variant
Dim i As Long, j As Long
Dim lr As Long, c As Long
Application.ScreenUpdating = False
With ActiveSheet
  .Columns("J:O").ClearContents
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  a = .Range("A1:G" & lr)
  ReDim o(1 To (lr - 1) * 3, 1 To 6)
  For i = 2 To lr
    For c = 4 To 6
      j = j + 1
      o(j, 1) = a(i, 1)
      o(j, 2) = a(i, 2)
      o(j, 3) = a(i, 3)
      o(j, 4) = a(1, c)
      o(j, 5) = a(i, c)
      o(j, 6) = a(i, 7)
    Next c
  Next i
  With .Range("J1").Resize(, 6)
    .Value = Array("Supplier", "Supplier Type", "Period", "Energy Type", "Value", "Production Share")
    .Font.Bold = True
  End With
  .Range("J2").Resize(UBound(o, 1), UBound(o, 2)).Value = o
  .Range("L2").Resize(UBound(o, 1)).NumberFormat = "mmm-d"
  .Range("N2").Resize(UBound(o, 1)).NumberFormat = "#,##0"
  .Range("O2").Resize(UBound(o, 1)).NumberFormat = "0%"
  .Columns("J:O").AutoFit
End With
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 ReorgData macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,636
Members
449,043
Latest member
farhansadik

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