In this post I will explain how to use DateSerial function in VBA. DateSerial function is a quite useful function we can use in VBA. If we input year, month and day this function returns the relevant date.
So we need to input three parameters to the function. All of them are mandatory.
Year - Integer type value which represents the year (ex - 2020)
Month - Integer type value which represents the month (ex - 10)
Day - Integer type value which represents the day (ex - 20)
Below example shows you how you can use this function in VBA
Sub DateSerialExample() Dim SalesDataDate As Date Dim iYear As Integer Dim iMonth As Integer Dim iDay As Integer iYear = 1965 iMonth = 10 iDay = 25 SalesDataDate = DateSerial(iYear, iMonth, iDay) Debug.Print SalesDataDate End Sub |
You will get this result if you run above macro
And if you want to change the format of the date then you can do it like this.
Sub DateSerialExample() Dim SalesDataDate As Date Dim iYear As Integer Dim iMonth As Integer Dim iDay As Integer iYear = 1965 iMonth = 10 iDay = 25 SalesDataDate = DateSerial(iYear, iMonth, iDay) Debug.Print Format(SalesDataDate, "dd-mmm-yyyy") End Sub |
So you will get following result.