lrobbo314
Well-known Member
- Joined
- Jul 14, 2008
- Messages
- 3,957
- Office Version
- 365
- Platform
- Windows
RANDOMDATE will return a number of random dates given a range of years.
If you omit the optional third argument, the function will return a single random date.
Inspired by a recent video by @MrExcel, video link.
I tried to adapt a VBA function to a LAMBDA function, but kept getting a "Nested Arrays" error whenever I tried to incorporate the MAP function in order to be able to return multiple dates.
Here is the function I was trying to use.
I would be interested to know if anyone would be able to tell me how you could make that one work.
But, @MrExcel's version was more elegant anyway.
If you omit the optional third argument, the function will return a single random date.
Inspired by a recent video by @MrExcel, video link.
Excel Formula:
RANDOMDATE
=LAMBDA(
year_start,year_end,[num_dates],
LET(
s,SEQUENCE(num_dates),
MAP(s,LAMBDA(x,DATE(RANDBETWEEN(year_start,year_end),1,RANDBETWEEN(1,365))))
)
)
RANDOMDATE | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | With all arguments entered | Without optional argument | |||||
2 | 2/28/2013 | 6/19/2019 | |||||
3 | 6/24/2009 | ||||||
4 | 6/20/2022 | ||||||
5 | 8/25/2022 | ||||||
6 | 1/12/2015 | ||||||
7 | 12/27/2020 | ||||||
8 | 5/4/2019 | ||||||
9 | 9/7/2017 | ||||||
10 | 2/5/2022 | ||||||
11 | 12/26/2011 | ||||||
Sheet2 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
A2:A11 | A2 | =RANDOMDATE(2008,2022,10) |
E2 | E2 | =RANDOMDATE(2008,2022,) |
Dynamic array formulas. |
I tried to adapt a VBA function to a LAMBDA function, but kept getting a "Nested Arrays" error whenever I tried to incorporate the MAP function in order to be able to return multiple dates.
Here is the function I was trying to use.
Excel Formula:
=LAMBDA(
year_start,year_end,
LET(
m,RANDBETWEEN(1,12),
y,RANDBETWEEN(year_start,year_end),
d,
IF(OR(m={1,3,5,7,8,10,12}),31,
IF(OR(m={4,6,9,11}),30,
IF(MOD(y,4)=0,
29,
28
)
)
),
DATE(y,m,d)
)
)
I would be interested to know if anyone would be able to tell me how you could make that one work.
But, @MrExcel's version was more elegant anyway.
Upvote
0