I think i've got it:
1st set up a spreadsheet with the following columns:
Start Time - End Time - Prob. Sum - 01:00 - 02:00 - 03:00 - etc to
00:00
Format the start time & end time columns to dd/mm/yyyy hh:mm (o
similar) to enable the input of combined date/time figures.
Put the prob. sum as "=SUM(D2:AA2)" for error checking
Select the 24 time columns on the row you want to inout values for an
insert the UDF I have written which takes the inputs StartTime an
EndTime. THis will then need to be array entered (ctrl+shift+ente
instead of just entered)- et voilla
The only issue you may encouter is if you have hundreds and hundreds o
these to calculate at once, you might want to go make a cup of te
while xl thinks.
Here's the code for the UDF though I have also attached a workboo
incase I lost you!
Duncan
Option Explicit
Function TimeProbability(dbStartTime As Double, dbEndTime As Double) A
Variant
Dim arTimeValues(23) As Single
Dim siProbability As Single
Dim siTimeDiff As Single
Dim inDays As Integer
Dim i As Integer, j As Integer
siTimeDiff = dbEndTime - dbStartTime
Select Case siTimeDiff
Case Is < 1
siProbability = 1 / (Hour(siTimeDiff) + 1)
If Application.WorksheetFunction.RoundDown(dbStartTime, 0)
Application.WorksheetFunction.RoundDown(dbEndTime, 0) Then
For i = Hour(dbStartTime) To Hour(dbEndTime)
arTimeValues(i) = siProbability
Next
Else
For i = Hour(dbStartTime) To 23
arTimeValues(i) = siProbability
Next
For i = 0 To Hour(dbEndTime)
arTimeValues(i) = siProbability
Next
End If
Case Is = 1
siProbability = (1 / 24)
For i = 0 To 23
arTimeValues(i) = siProbability
Next
Case Is > 1
inDays = Application.WorksheetFunction.RoundDown(dbEndTime, 0
- Application.WorksheetFunction.RoundDown(dbStartTime, 0)
siProbability = 1
((Application.WorksheetFunction.RoundDown(dbEndTime - dbStartTime, 0)
24) _
+ (Hour(dbEndTime) - Hour(dbStartTime) + 1))
For i = Hour(dbStartTime) To 23
arTimeValues(i) = arTimeValues(i) + siProbability
Next
For i = 0 To Hour(dbEndTime)
arTimeValues(i) = arTimeValues(i) + siProbability
Next
If Hour(dbEndTime) >= Hour(dbStartTime) Then
For j = 1 To inDays - 1
For i = 0 To 23
arTimeValues(i) = arTimeValues(i) + siProbability
Next i
Next j
Else
For j = 1 To inDays - 2
For i = 0 To 23
arTimeValues(i) = arTimeValues(i) + siProbability
Next i
Next j
End If
End Select
TimeProbability = arTimeValues
End Functio
Attachment filename: timeanalysis.xls
Download attachment:
http://www.excelforum.com/attachment.php?postid=54940