VBA macro HELP- Column to rows

kmm992

New Member
Joined
Aug 17, 2018
Messages
1
Hi there!

I am in need of some VBA macro magic here. I have a stream of data that represents energy usage on an hourly basis. It is in one column currently that goes Day 1 HE 1, Day 1 HE 2, etc.

I would like to have a macro that takes the 24 hours and transposes it into a table that has the days of the month on the right going down and the data in rows with hours 1-24 being the top header.

Basically it would be a macro that transposes the 24 hourly data points for each day 31 times from a single column to a series of rows

from this:
Hourdata
1
8756
2425
3452
4452
5425
64345
7453
8453
9453
10453
11453
12453
13453
14453
15786
1778453
187532
19735
20735
21753
22753
23753
24735

<tbody>
</tbody>

To this:
he123456789101112131415161718192021222324
day5646564654564664554654654654654654654654645654654654654654456546546546546

<tbody>
</tbody>


I realize the numbers dont match just wanted to show an example. This would be done for the whole month in this case with the days going down in rows.

I would very much appreciate the help!
 

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.
this code should do it for you:
Code:
Sub test()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow + 1, 2))
Range(Cells(1, 3), Cells(32, 28)) = ""
outarr = Range(Cells(1, 3), Cells(32, 28))
ci = 1
ri = 2
For i = 2 To lastrow
   outarr(1, ci) = inarr(i, 1)
   outarr(ri, ci) = inarr(i, 2)
   ci = ci + 1
   If inarr(i + 1, 1) < inarr(i, 1) Then
    ri = ri + 1
    ci = 1
   End If
   
 Next i
Range(Cells(1, 3), Cells(32, 28)) = outarr
 
 End Sub
 
Upvote 0

Forum statistics

Threads
1,215,382
Messages
6,124,618
Members
449,175
Latest member
Anniewonder

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