×

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number.

Syntax

DateSerial (Year, Month, Day)

Parameter

Year (required) – This parameter represents an integer signifying the year.

Month (required) – This parameter represents an integer signifying the month. If the Integer values are less than 1 or greater than 12, the values are interpreted as:

  • -1 – November of the previous year
  • 0 – November of previous year
  • 13 – January of the following year
  • 14 – February of the following year

Day (required) – This parameter represents an integer signifying the day of the month. If the integer values less are than 1 or greater than the number of days in the current month are interpreted as follows:

  • -1 – It signifies the second to the last day of the previous month
  • 0 – It signifies the last day of the previous month.
  • Days in current month + 1 – It signifies the first day of the following month
  • Days in current month + 2 – It signifies the second day of the following month.

Return

This function returns a Date from a supplied year, month, and day number.

Example 1

Sub DateSerialFunction_Example1()
 ' Two different ways to return the date "12/31/2015"
 Dim date_val1 As Date, date_val2 As Date
 date_val1 = DateSerial(2020, 6, 31)
 date_val2 = DateSerial(20, 6, 31)
 ' The variables date_val1 and date_val2 are equal to the Date 12/31/2015. 
 Cells(1, 1).Value = date_val1
 Cells(2, 1).Value = date_val2
 End Sub 

Output

7/1/2020
7/1/2020
VBA DateSerial Function

Example 2

Sub DateSerialFunction_Example2()
 ' Demonstrating two different ways to return the date "12/31/2020"
 Dim date_val1 As Date, date_val2 As Date
 date_val1 = DateSerial(2020, 12, 31)
 date_val2 = DateSerial(2020, 13, 31)
 ' The variables date_val1 will return 12/31/2020
 ' The date_val2 are equal to the Date 1/31/2021. 
 Cells(1, 1).Value = date_val1
 Cells(2, 1).Value = date_val2
 End Sub 

Output

12/31/2020
1/31/2021

Example 3

Sub DateSerialFunction_Example3()
 ' Demonstrating two different ways to use Day Numbers That Are Less Than 1 Or Greater Than 31
 Dim date_val1 As Date, date_val2 As Date
 date_val1 = DateSerial(2020, 12, 1)
 date_val2 = DateSerial(2020, 12, 32)
 ' The variables date_val1 will return 12/1/2020 
 ' The date_val2 are equal to the Date 1/1/2021.
 Cells(1, 1).Value = date_val1
 Cells(2, 1).Value = date_val2
 End Sub 

Output

12/1/2020
1/1/2021
VBA DateSerial Function

Related Topics

Excel VBA INT Function

VBA INT Function: The INT function in VBA rounds the given supplied number down and returns an integer value. The positive numbers are rounded to zero, and the negative numbers are rounded away from zero. Syntax Int...

1 minute read.

Excel VBA DatePart Function

The DatePart function in VBA returns a part (day, month, week, etc.) for the specified date and/or time. Syntax DatePart (Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required) – This parameter represents a string specifying the interval to be used. It can...

2 minutes read.

CodeIgniter Architecture

Here we will understand the architecture and working of the CodeIgniter application, which helps you to elaborate all steps in simple ways. As the above image represents that whenever a request comes from the...

2 minutes read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

Excel VBA Minute Function

The Minute function in VBA returns the minute component for the specified time. Syntax Minute (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the minute component for the specified time. Example 1 Sub MinteFunction_Example1() ...

1 minute read.

Excel VBA Replace Function

VBA Replace Function: The Replace function in VBA searches for a substring within the specified string and replaces its occurrences with a second substring. Syntax Replace (Expression, Find, Replace, [Start], [Count], [Compare]) Parameter Expression (required) – This parameter represents...

2 minutes read.

Excel VBA Weekday Function

Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date. Syntax Weekday (Date, [FirstDayOfWeek]) Parameter Date (required) – This parameter...

2 minutes read.

Excel VBA Hour Function

The hour function in VBA returns the hour element for the specified time. Syntax Hour (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the hour element for the specified time. Example 1 Sub HourFunction_Example1() ...

1 minute read.

VBA Updating Pivot Table

VBA- Updating Pivot Table The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful...

3 minutes read.

Excel VBA: IF…..THEN …ELSE Statement

VBA : IF…..THEN …ELSE Statement: This function enables you to check one condition and, based on that, then run one of the two statement blocks present. If the ‘IF’ condition...

2 minutes read.

Excel VBA: DO WHILE….Loop

DO WHILE….Loop The “Do While” Loop is the same, unlike the FOR statement, just that it will keep on looping till the specified condition is true. It is used when we want to...

3 minutes read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

VBA Pivot Table

What is a Pivot Table? One of the most powerful features of Excel VBA is the Pivot Table. A pivot table is a VBA tool that is used to create summary...

3 minutes read.

VBA Charts Basic Operations

VBA Charts- Basic Operations The Chart Object in Excel VBA represents the collection of all the charts sheet present in a workbook. A chart can be either an embedded chart or a separate chart sheet. The...

6 minutes read.

Four VBA Clear methods

Four VBA Clear methods In Microsoft Excel, many times, a situation arises where the user wants to clear the data or any specific range of data. What if the user automates this task with...

6 minutes read.

Excel VBA WeekdayName Function

Excel VBA WeekdayName Function: The WeekdayName function in VBA returns a string containing the weekday name, for the specified integer representation of a weekday. Syntax WeekdayName (Weekday, [Abbreviate], [FirstDayOfWeek]) Parameter Weekday (required) – This parameter represents...

2 minutes read.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.

Excel VBA CCur Function

The CCur function in VBA is used to convert an expression into a Currency data type. It can take a maximum of 15 digits to the left of the decimal place and...

1 minute read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.