Forum Discussion

mok1995's avatar
mok1995
Copper Contributor
Jun 30, 2021
Solved

Date Increasing

Hello all I have an excel sheet containing information for many machines that needs maintenance, each machine has a starting date of maintenance and the maintenance will be done repetitively in a sp...
  • HansVogelaar's avatar
    HansVogelaar
    Jun 30, 2021

    mok1995 

    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 Function

    Let'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.

Resources