Forum Discussion
Date Increasing
- Jun 30, 2021
Custom function, to be copied into a new module in the Visual Basic Editor:
Function NextDate(DateLast As Date, Schedule As String, DateFrom As Date) As Date Dim n As Long Dim m As Long Select Case Schedule Case "D" NextDate = DateFrom Case "W" n = (DateFrom - DateLast) \ 7 NextDate = DateLast + 7 * n If NextDate < DateFrom Then NextDate = NextDate + 7 End If Case "W2" n = (DateFrom - DateLast) \ 14 NextDate = DateLast + 14 * n If NextDate < DateFrom Then NextDate = NextDate + 14 End If Case "M" n = DateDiff("m", DateLast, DateFrom) NextDate = DateAdd("m", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("m", n + 1, DateLast) End If Case "NM:2", "NM:3", "NM:4", "NM:5", "NM:6", "NM:7", "NM:8", "NM:9", "NM:10", "NM:11" m = Split(Schedule, ":")(1) n = m * (DateDiff("m", DateLast, DateFrom) \ m) NextDate = DateAdd("m", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("m", n + m, DateLast) End If Case "NY:1" n = DateDiff("yyyy", DateLast, DateFrom) NextDate = DateAdd("yyyy", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("yyyy", n + 1, DateLast) End If Case "NY:2" n = DateDiff("yyyy", DateLast, DateFrom) If n Mod 2 Then n = n - 1 NextDate = DateAdd("yyyy", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("yyyy", n + 2, DateLast) End If End Select End FunctionLet's say you enter a date in N2.
Enter the following formula in K2:
=NextDate(J2,I2,$N$2)
Fill down to the last row with data.
You can now easily filter the data on column K for today or next month or whatever.
See the attached workbook, now a macro-enabled workbook. Make sure that you allow macros when you open it.
Does D mean daily?
HansVogelaar yes, D is daily
- HansVogelaarJun 30, 2021MVP
Custom function, to be copied into a new module in the Visual Basic Editor:
Function NextDate(DateLast As Date, Schedule As String, DateFrom As Date) As Date Dim n As Long Dim m As Long Select Case Schedule Case "D" NextDate = DateFrom Case "W" n = (DateFrom - DateLast) \ 7 NextDate = DateLast + 7 * n If NextDate < DateFrom Then NextDate = NextDate + 7 End If Case "W2" n = (DateFrom - DateLast) \ 14 NextDate = DateLast + 14 * n If NextDate < DateFrom Then NextDate = NextDate + 14 End If Case "M" n = DateDiff("m", DateLast, DateFrom) NextDate = DateAdd("m", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("m", n + 1, DateLast) End If Case "NM:2", "NM:3", "NM:4", "NM:5", "NM:6", "NM:7", "NM:8", "NM:9", "NM:10", "NM:11" m = Split(Schedule, ":")(1) n = m * (DateDiff("m", DateLast, DateFrom) \ m) NextDate = DateAdd("m", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("m", n + m, DateLast) End If Case "NY:1" n = DateDiff("yyyy", DateLast, DateFrom) NextDate = DateAdd("yyyy", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("yyyy", n + 1, DateLast) End If Case "NY:2" n = DateDiff("yyyy", DateLast, DateFrom) If n Mod 2 Then n = n - 1 NextDate = DateAdd("yyyy", n, DateLast) If NextDate < DateFrom Then NextDate = DateAdd("yyyy", n + 2, DateLast) End If End Select End FunctionLet's say you enter a date in N2.
Enter the following formula in K2:
=NextDate(J2,I2,$N$2)
Fill down to the last row with data.
You can now easily filter the data on column K for today or next month or whatever.
See the attached workbook, now a macro-enabled workbook. Make sure that you allow macros when you open it.
- mok1995Jun 30, 2021Copper Contributorthank you dear, it works perfectly
thank you very very much - mok1995Jun 30, 2021Copper Contributoris the dates will be updated daily ?
please can you do it for sheet 2 in same file please, i need it for the two sheets
please
so everyday the dates will be updated right ?- HansVogelaarJun 30, 2021MVP
See the attached version.
The result will be updated automatically each day. (I entered the formula =TODAY() in N2)