Shift Data From Row To Column

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,621
Office Version
  1. 2021
Platform
  1. Windows
Code:
Would someone please help me make a VBA script to do this. See photo below. All data is consistent I have bout 500 rows of data.
OkFG3Vo.png
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
My Aswer Is This,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


You are posting a picture. This means that if this was a problem where one needed to use your data, anyone trying to help you would have to enter the data manually. That makes no sense.


Can you post a screenshot of the actual raw data worksheet?

And, can you post a screenshot of the worksheet results (manually formatted by you) that you are looking for?

To post your data, you can download and install one of the following two programs:
1. MrExcel HTMLMaker20101230
https://onedrive.live.com/?cid=8cffdec0ce27e813&sc=documents&id=8CFFDEC0CE27E813!189

Installation instructions here:
http://www.mrexcel.com/forum/board-announcements/515787-forum-posting-guidelines.html#post2545970

2. Excel Jeanie
Download


If you are not able to give us screenshots:
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
Assumes your Date header is in A1, and each read date has 3 data points. Rearrange data begin with date header in E1 on same sheet.
Code:
Sub RearrangeData()
'Assumes read date data in groups of 3 rows each and first read date in cell A2
Dim Hdrs As Variant, lR As Long, Rws As Long, i As Long
Application.ScreenUpdating = False
Hdrs = Array("Read Date", "OFF PEAK", "ON PEAK", "SHLDR PEAK")
Range("E1:H1").Value = Hdrs
lR = Range("A" & Rows.Count).End(xlUp).Row
Rws = Range("A2:A" & lR).Rows.Count
If Rws / 3 <> Int(Rws / 3) Then
    MsgBox "Must be three data points per read date"
    Exit Sub
End If
For i = 1 To Rws Step 3
    lR = Range("E" & Rows.Count).End(xlUp).Row + 1
    Range("E" & lR).Value = Range("A1").Offset(i, 0)
    Range("F" & lR, "H" & lR).Value = _
        WorksheetFunction.Transpose(Range("B1").Offset(i, 0).Resize(3, 1))
Next i
Columns("E:H").AutoFit
Application.ScreenUpdating = True
End Sub
 
Upvote 0
My Aswer Is This,

Sample raw data:


Excel 2007
ABCDEFGHI
1Read DateConsumption AmountReading Type
27/29/2014822 kWhOFF PEAK
37/29/2014126 kWhON PEAK
47/29/201468 kWhSHLDR PEAK
56/26/2014673 kWhOFF PEAK
66/26/2014116 kWhON PEAK
76/26/201469 kWhSHLDR PEAK
8
Sheet1


After the macro:


Excel 2007
ABCDEFGHI
1Read DateConsumption AmountReading TypeRead DateOFF PEAKON PEAKSHLDR PEAK
27/29/2014822 kWhOFF PEAK7/29/2014822 kWh126 kWh68 kWh
37/29/2014126 kWhON PEAK6/26/2014673 kWh116 kWh69 kWh
47/29/201468 kWhSHLDR PEAK
56/26/2014673 kWhOFF PEAK
66/26/2014116 kWhON PEAK
76/26/201469 kWhSHLDR PEAK
8
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, 08/03/2014, ME796232
Dim lr As Long, c As Range, drng As Range, trng As Range
Application.ScreenUpdating = False
Columns("F:I").ClearContents
Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Columns(6), Unique:=True
Cells(1, 7).Resize(, 3).Value = Array("OFF PEAK", "ON PEAK", "SHLDR PEAK")
lr = Cells(Rows.Count, 1).End(xlUp).Row
For Each c In Range("A2:A" & lr)
  Set drng = Columns(6).Find(c, LookAt:=xlWhole)
  Set trng = Rows(1).Find(c.Offset(, 2).Value, LookAt:=xlWhole)
  If (Not drng Is Nothing) * (Not trng Is Nothing) Then
    Cells(drng.Row, trng.Column).Value = c.Offset(, 1).Value
  End If
Next c
Columns("F:I").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 ReorgData macro.
 
Upvote 0
Thank you hiker95 . I tried JoeMoe's suggestion and it worked perfect. Appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,223,701
Messages
6,173,920
Members
452,538
Latest member
deeme

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