DateAdd Function

     

  • It adds\subtracts time from a particual date
  • We can do this by using below mentioned format
  •  

     

    Syntax explanation:

  • Interval: mention in terms of Years,months,weeks,hours,days
  • Number: Mention the number applicable to Interval
  • Date: to which date we have to add the Interval
  •  

    Code:

    Private Sub CommandButton1_Click()
    Add 2 years to 2016
    MsgBox DateAdd("yyyy", 2, "1-jan-2016")
    Add 2 years to 2016
    Range("A1").Value = DateAdd("yyyy", 2, "1-oct-2016")
    deduct 2 years to 2016
    Range("A2").Value = DateAdd("yyyy", -2, "1-oct-2016")
    deduct 2 weeks to 2016
    Range("A3").Value = DateAdd("ww", -2, "1-oct-2016")
    Deduct 2 weekdays to 2016
    Range("A4").Value = DateAdd("w", -2, "1-oct-2016")
    Deduct 2 days to 2016
    Range("A5").Value = DateAdd("d", -2, "1-oct-2016")
    Add 45 months to 2016
    Range("A6").Value = DateAdd("m", 45, "1-oct-2016")
    Add 3 hours to 2016
    Range("A7").Value = DateAdd("h", 3, "1-oct-2016")
    End Sub

    Download The Workbook

     

    DateAdd Function - Serial Dates

    Code:

    Private Sub CommandButton1_Click()
    Dim i As Integer
    'adding 100 days
    For i = 1 To 100
    Cells(i, 1).Value = DateAdd("d", i, "1-1-2016")
    Next
    End Sub

    Download The Workbook