The VBA DATESERIAL function is listed under the date category of VBA functions. When you use it in a VBA code, it returns a valid date as per VBA’s date format by the supplied day, month, and year. In simple words, you can create a date with day, month, and year input. It’s just like the DATE function in the worksheet.
Syntax
DateSerial(Year, Month, Day)
Arguments
- Year: An integer to use as the year in the date.
- Month: An integer to use as the month in the date.
- Day: An integer to use as the day in the date.
Example
To practically understand how to use the VBA DATESERIAL function, you need to go through the below example where we have written a vba code by using it:
Sub example_DATESERIAL()
Range("A1").Value = DateSerial(2019, 5, 15)
End Sub
In the above example, we have used the DATESERIAL to create a date with 2019 as a year, 5 months, and 15 as a day. As a result, it has returned 15-May-19.
Notes
- Make sure to use the four-digit value for the years.
- Adding the day number out of the maximum number of days in a month will return the date by adding excess days to the next month.
- Adding the month number out of the maximum number of months in a year will return the date by adding excess months to the next year.
- Back to the Excel VBA / VBA Functions