How to calculate average spending time by using VBA?

AngelicaWan

New Member
Joined
Aug 20, 2015
Messages
8
How to use VBA to calculate the average time of this list. And how to calculate the difference between each time and average time? :confused:

Thanks a lot!

Like there's a list of time:
00:01:28
00:01:23
00:00:00
00:01:01
00:05:07
00:00:00
00:00:00
00:02:45
00:03:14
00:01:14
00:00:00
00:00:44
00:00:00
00:00:46
00:01:19
00:00:12
00:00:37
00:00:52
00:00:34
00:00:00
00:02:16
00:01:28




<colgroup><col style="width: 48pt;" width="64">
<tbody>


</tbody>
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Well you could do that with just an excel formula: =AVERAGE(A1:A22)

However, if you're feeling like you'd prefer VBA, I'm sure this would work:
Code:
Sub averagetime()


Range("A23") = WorksheetFunction.Average(Range("A1:A22"))


End Sub
 
Upvote 0
Thank you nicknep!:biggrin:

I was failed when I tried at the first time. Then I reliazed I may write it under module not under particular sheet.
 
Upvote 0
Hey, no problem! I just now read the second part of your question and worked out something for that too:

Code:
Sub AverageTime()
Cells(1, 1).Select
lr = Selection.End(xlDown).Row 'finds last row
Cells(lr + 1, 1) = WorksheetFunction.Average(Range("A1:A" & lr))
For x = 1 To lr
If Cells(x, 1) > Cells(lr + 1, 1) Then Cells(x, 2) = Cells(x, 1) - Cells(lr + 1, 1)
If Cells(x, 1) < Cells(lr + 1, 1) Then Cells(x, 2) = Cells(lr + 1, 1) - Cells(x, 1)
Next x
End Sub

Assuming your data starts in cell A1 and is in column A, this macro finds the last row of your data and averages all the times. It then takes the difference of the average and each time and outputs it in the adjacent cell. I ran into a small problem where a time can't be negative, so it first checks to see which time is larger, then subtracts by that first.

I hope this helps!
 
Upvote 0
Thank you so much nicknep! Awesome! That is a great help!

I just noticed the reply and I also realized the one with the negative difference. So I marked them with different font color.

However new issue comes. I couldn't successfully run the codes with the following number list. I made the format as [h]:mm:ss and that still does not work. So how to do with the ones larger than 12:00:00?

02:38:47
09:00:56
14:02:04
08:06:19
05:30:08
06:34:24
00:00:00
04:03:56
00:00:00
06:40:55
00:00:00
13:04:09
00:22:32
03:17:23
06:22:27
07:17:58
04:14:10
07:22:32
02:27:45
03:11:29
10:50:05
07:07:58
08:33:04
00:00:00
00:07:27
03:39:37
04:15:26



<colgroup><col style="width: 48pt;" width="64">
<tbody>


</tbody>


Thank you!
 
Upvote 0
Oh, I know a little trick here. When the time is larger than 12:00:00, you can copy the data to a word document with value only format. Then copy back to spreadsheet is OK.

But I still don't know what to do with the ones like:

1:01:20:27
1:04:04:02

<tbody>
</tbody><colgroup><col></colgroup>

<tbody>
</tbody><colgroup><col></colgroup>
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,969
Members
449,059
Latest member
oculus

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