Hello everybody,
I am writing a vba function that assigns a value of 10 * a probability to each calendar date in the future, only if that date is within -5<date<5 the next closest date from the EVENTS list (on the left below the excel example). So if I am on the 17/04/2018 and the closest EVENTS date is the 18/04/2018, the function will assign a value of 5 (10*EVENTS probability). The 15/05/2018 will have a value of 6 ( closest EVENT 18/05/2018, prob 60%). A date like 25/04/2018 will have a value of 0 as it is not within 5 days of any EVENTS date. I can't figure out how to use the correct probability in the VBA function. Below what I wrote. I am an absolute beginner of VBA but I am learning so much from this forum. Any very simple solution to this would be soon much appreciated guys!
Function IncreaseT(t As Integer, prob As Range)
' t is the calendar date
' prob is the probability assigned to each EVENTS date
If t > 5 Or t < -5 Then
IncreaseT = 0
Else: IncreaseT = 10 * prob
Exit Function
[TABLE="width: 536"]
<colgroup><col span="4"><col></colgroup><tbody>[TR]
[TD]EVENTS[/TD]
[TD]Probability[/TD]
[TD][/TD]
[TD]Calendar[/TD]
[TD]Distance from closest Event[/TD]
[/TR]
[TR]
[TD]18/04/18[/TD]
[TD]50%[/TD]
[TD][/TD]
[TD]15/04/18[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]18/05/18[/TD]
[TD]60%[/TD]
[TD][/TD]
[TD]16/04/18[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]20/08/18[/TD]
[TD]50%[/TD]
[TD][/TD]
[TD]17/04/18[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]02/11/18[/TD]
[TD]80%[/TD]
[TD][/TD]
[TD]18/04/18[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]24/02/19[/TD]
[TD]20%[/TD]
[TD][/TD]
[TD]19/04/18[/TD]
[TD]-1[/TD]
[/TR]
[TR]
[TD]29/05/19[/TD]
[TD]30%[/TD]
[TD][/TD]
[TD]20/04/18[/TD]
[TD]-2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]21/04/18[/TD]
[TD]-3[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]22/04/18[/TD]
[TD]-4[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]23/04/18[/TD]
[TD]-5[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]24/04/18[/TD]
[TD]-6[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]25/04/18[/TD]
[TD]-7[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]26/04/18[/TD]
[TD]-8[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]27/04/18[/TD]
[TD]-9[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]28/04/18[/TD]
[TD]-10[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]29/04/18[/TD]
[TD]-11[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]30/04/18[/TD]
[TD]-12[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]01/05/18[/TD]
[TD]-13[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]02/05/18[/TD]
[TD]-14[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]03/05/18[/TD]
[TD]-15[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]04/05/18[/TD]
[TD]14[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]05/05/18[/TD]
[TD]13[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]06/05/18[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]07/05/18[/TD]
[TD]11[/TD]
[/TR]
[TR]
[TD][/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[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]24/05/18[/TD]
[TD]-6[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]25/05/18[/TD]
[TD]-7[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]26/05/18[/TD]
[TD]-8[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]27/05/18[/TD]
[TD]-9[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]28/05/18[/TD]
[TD]-10[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]29/05/18[/TD]
[TD]-11[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]30/05/18[/TD]
[TD]-12[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]31/05/18[/TD]
[TD]-13[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]01/06/18[/TD]
[TD]-14[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]02/06/18[/TD]
[TD]-15[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]03/06/18[/TD]
[TD]-16[/TD]
[/TR]
</tbody>[/TABLE]
I am writing a vba function that assigns a value of 10 * a probability to each calendar date in the future, only if that date is within -5<date<5 the next closest date from the EVENTS list (on the left below the excel example). So if I am on the 17/04/2018 and the closest EVENTS date is the 18/04/2018, the function will assign a value of 5 (10*EVENTS probability). The 15/05/2018 will have a value of 6 ( closest EVENT 18/05/2018, prob 60%). A date like 25/04/2018 will have a value of 0 as it is not within 5 days of any EVENTS date. I can't figure out how to use the correct probability in the VBA function. Below what I wrote. I am an absolute beginner of VBA but I am learning so much from this forum. Any very simple solution to this would be soon much appreciated guys!
Function IncreaseT(t As Integer, prob As Range)
' t is the calendar date
' prob is the probability assigned to each EVENTS date
If t > 5 Or t < -5 Then
IncreaseT = 0
Else: IncreaseT = 10 * prob
Exit Function
[TABLE="width: 536"]
<colgroup><col span="4"><col></colgroup><tbody>[TR]
[TD]EVENTS[/TD]
[TD]Probability[/TD]
[TD][/TD]
[TD]Calendar[/TD]
[TD]Distance from closest Event[/TD]
[/TR]
[TR]
[TD]18/04/18[/TD]
[TD]50%[/TD]
[TD][/TD]
[TD]15/04/18[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]18/05/18[/TD]
[TD]60%[/TD]
[TD][/TD]
[TD]16/04/18[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]20/08/18[/TD]
[TD]50%[/TD]
[TD][/TD]
[TD]17/04/18[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]02/11/18[/TD]
[TD]80%[/TD]
[TD][/TD]
[TD]18/04/18[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]24/02/19[/TD]
[TD]20%[/TD]
[TD][/TD]
[TD]19/04/18[/TD]
[TD]-1[/TD]
[/TR]
[TR]
[TD]29/05/19[/TD]
[TD]30%[/TD]
[TD][/TD]
[TD]20/04/18[/TD]
[TD]-2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]21/04/18[/TD]
[TD]-3[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]22/04/18[/TD]
[TD]-4[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]23/04/18[/TD]
[TD]-5[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]24/04/18[/TD]
[TD]-6[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]25/04/18[/TD]
[TD]-7[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]26/04/18[/TD]
[TD]-8[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]27/04/18[/TD]
[TD]-9[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]28/04/18[/TD]
[TD]-10[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]29/04/18[/TD]
[TD]-11[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]30/04/18[/TD]
[TD]-12[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]01/05/18[/TD]
[TD]-13[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]02/05/18[/TD]
[TD]-14[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]03/05/18[/TD]
[TD]-15[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]04/05/18[/TD]
[TD]14[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]05/05/18[/TD]
[TD]13[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]06/05/18[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]07/05/18[/TD]
[TD]11[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]08/05/18[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]09/05/18[/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]10/05/18[/TD]
[TD]8[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]11/05/18[/TD]
[TD]7[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]12/05/18[/TD]
[TD]6[/TD]
[/TR]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/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]29/05/18[/TD]
[TD]-11[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]30/05/18[/TD]
[TD]-12[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]31/05/18[/TD]
[TD]-13[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]01/06/18[/TD]
[TD]-14[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]02/06/18[/TD]
[TD]-15[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]03/06/18[/TD]
[TD]-16[/TD]
[/TR]
</tbody>[/TABLE]