VBA to Calculate Median/Average of 134,000 Rows (Separated by Hundredths of a Second)

BarGraph

New Member
Joined
May 11, 2018
Messages
2
Hi All,

I have a spreadsheet with 134,000 rows. Each row contains data from a hundredth of a second (1343 seconds total).
Each hundredth of a second has several points of data that was recorded. What I would like to do is, summarize the data by second. (Average & Median of each Data 1, Data 2, Data 3 for each second.)

Example of Data:
TimeData 1Data 2Data 3
.001111
.002234
.003556
1336

<tbody>
</tbody>

I have already set a floor.
Ex:
TimeFloorData 1Data 2Data 3
.0010111
.0020234
.0030556
11336

<tbody>
</tbody>

Ideally, it would look something like this when done:
TimeFloorData 1Data 2Data 3FloorData 1 Aver.Data 2 Aver.Data Three Aver.
.001011102.66633.66
.00202341136
.0030556
11336

<tbody>
</tbody>

I've tried writing the following code:
Code:
Private Sub cmdRun_Click()
'Declare variables
Dim x As String
Dim row As Integer
Dim lastSecondInFloor
Dim time As String
Dim floor As String
Dim newFloor As String
Dim Data1 As String
Dim Data2 As String
Dim Data3 As String


'Get floor
x = 0
row = 3
floor = Cells(row, 2)
Do
floor = Cells(row, 2)
If floor <> x Then
x = x + 1
'The commented out line below seemed to work for inserting a message into the last line in a "floor"
'I just don't know how to make it take the median/average of whole floor
'Cells(row - 1, 10) = "last"
lastSecondInFloor = row - 1
'This code below didn't work as expected
Cells(row - 1, 10) = WorksheetFunction.Median("lastSecondInFloor,8:row,8")


End If


row = row + 1
'last second number to be found goes here
Loop While x <= 1343




End Sub
Let me know what you guys think. I am stumped on how to get VBA to take each floor "chunk" and do the calculation. To make things worse, not every second of data contains the same number of datapoints (eg. sometimes there would be multiple seconds repeated) so I can't break the data up every hundred rows.

Thanks for reading!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Your results suggest you are using mean not median. For that reason id definately be using a pivot table for this. Nothing else will compete for speed over that many rows.
 
Upvote 0
Hi Steve,

Thank you for your response! I would like to find mean and also median as well for each second, I just listed mean in my example to keep my message as short as possible. Sorry for the confusion!

I did initially try using a pivot table, but was unable to get it work correctly.
I assigned the Floors to be rows and attempted to group them, but the only way to get it to group appeared to be by increments. But each Floor contains differing amounts of datarows.

Any idea how to group correctly?
 
Upvote 0
You can get your floor column using roundup formula. However median is as far as I'm aware not possible with a pivot. Mean is ok. You then need to put floor in the rows part of the pivot and your data1 data2 data3 in the totals part. Click them one by one and change them to average.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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