VBA function find closest date from list

gero92

New Member
Joined
Apr 15, 2018
Messages
10
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


EVENTSProbabilityCalendarDistance from closest Event
18/04/1850%15/04/18
18/05/1860%16/04/182
20/08/1850%17/04/181
02/11/1880%18/04/180
24/02/1920%19/04/18-1
29/05/1930%20/04/18-2
21/04/18-3
22/04/18-4
23/04/18-5
24/04/18-6
25/04/18-7
26/04/18-8
27/04/18-9
28/04/18-10
29/04/18-11
30/04/18-12
01/05/18-13
02/05/18-14
03/05/18-15
04/05/1814
05/05/1813
06/05/1812
07/05/1811
08/05/1810
09/05/189
10/05/188
11/05/187
12/05/186
13/05/185
14/05/184
15/05/183
16/05/182
17/05/181
18/05/180
19/05/18-1
20/05/18-2
21/05/18-3
22/05/18-4
23/05/18-5
24/05/18-6
25/05/18-7
26/05/18-8
27/05/18-9
28/05/18-10
29/05/18-11
30/05/18-12
31/05/18-13
01/06/18-14
02/06/18-15
03/06/18-16

<colgroup><col span="4"><col></colgroup><tbody>
</tbody>
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
The initial explanation was cut as too long I suppose. The thing is that the probability in the vba function has to be the one assigned in the excel to each date in the list. So for the calendar date ex. 15/05/2018 I want the prob to be 60% (closest to 18/05/2018 date in EVENT list), while for let's say the 25/08/2018 the prob will be 50% (closest the third date in EVENT). The t in the vba function is the distance of each calendar date to the closest date in the EVENT list from excel. Hope it is clearer now! Anybody who could help please?
 
Upvote 0
How about just a formula?

A​
B​
C​
D​
E​
1​
EVENTS
Probability
Calendar
Prob
2​
18 Apr 2018​
50%​
15 Apr 2018​
50%​
3​
18 May 2018​
60%​
16 Apr 2018​
50%​
4​
20 Aug 2018​
50%​
17 Apr 2018​
50%​
5​
02 Nov 2018​
80%​
18 Apr 2018​
50%​
6​
24 Feb 2019​
20%​
19 Apr 2018​
50%​
7​
29 May 2019​
30%​
20 Apr 2018​
50%​
8​
21 Apr 2018​
50%​
9​
22 Apr 2018​
50%​
10​
23 Apr 2018​
50%​
11​
24 Apr 2018​
0%​
12​
25 Apr 2018​
0%​
13​
26 Apr 2018​
0%​
14​
27 Apr 2018​
0%​
15​
28 Apr 2018​
0%​
16​
29 Apr 2018​
0%​
17​
30 Apr 2018​
0%​
18​
01 May 2018​
0%​
19​
02 May 2018​
0%​
20​
03 May 2018​
0%​
21​
04 May 2018​
0%​
22​
05 May 2018​
0%​
23​
06 May 2018​
0%​
24​
07 May 2018​
0%​
25​
08 May 2018​
0%​
26​
09 May 2018​
0%​
27​
10 May 2018​
0%​
28​
11 May 2018​
0%​
29​
12 May 2018​
0%​
30​
13 May 2018​
60%​
31​
14 May 2018​
60%​
32​
15 May 2018​
60%​

The array formula in E2 and copied down is

Code:
=IF(ABS(INDEX($A$2:$A$7, MATCH(MIN(ABS($A$2:$A$7 - D2)), ABS($A$2:$A$7 - D2), 0)) - D2) > 5, 0, 
        INDEX($B$2:$B$7, MATCH(MIN(ABS($A$2:$A$7 - D2)), ABS($A$2:$A$7 - D2), 0)))
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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