×

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

VBA Creating, Displaying, Uploading UserForms

UserForm is a customized interface and acts as a VBA container and can add various controls as per the required functionality, each of which has certain usage and related properties. You can...

4 minutes read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

Debugging in Excel VBA

Debugging in VBA: Debugging is a technique used to fix errors in programming languages. In Excel VBA, we have different ways by which you can identify the error in the...

2 minutes read.

Excel VBA : With End-with Constructs

With End-with Constructs The With-End With construct enables the user to perform multiple operations on a single object. If you are going to perform several different actions on the same object and typing the same...

4 minutes read.

Excel VBA Sgn Function

VBA Sgn Function: The Sgn function in VBA returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number. Syntax Sgn (Number) Parameter Number (required) –This parameter represents the number that...

1 minute read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

Excel VBA CStr Function

VBA CStr Function: The CStr function in VBA converts an expression into a string data type. Syntax CStr (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA IsEmpty Function

VBA IsEmpty Function: The IsEmpty function in VBA returns a Boolean value showing whether the specified Expression is Empty (variant has not been declared) or not. Syntax IsEmpty (Expression) Parameter Expression (required)- This parameter...

1 minute read.

Excel VBA FormatNumber Function

VBA FormatNumber Function: The FormatNumber function in VBA is used to apply a number format to a numeric expression, and it returns the result as a string. Syntax FormatNumber (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes 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.

Excel VBA CVar Function

VBA CVar Function: The CVar function in VBA converts an expression into a Variant data type. Syntax CVar (Expression) Parameter Expression (required) – This parameter represents the expression that that you want to convert...

1 minute read.

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.

Excel VBA UBound Function

Excel VBA UBound Function: The UBound function in VBA returns the highest subscript for the specified dimension in the given array. Syntax UBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

Excel VBA: Select … Case Statement

Select … Case Statement When a group of statements is executed, depending upon the value of an Expression, then Switch Case is used.  If you have several conditions to check, then the If condition...

4 minutes 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 Array

Introduction to VBA Array An array is a type of variable that holds more than one piece of data. In VBA, you can refer to a specific variable (element) of an array by using...

5 minutes read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

Excel VBA While wend Loop

WHILE wend loop is used when the user is not sure how many times they want to execute the VBA code within the program. With a WHILE loop, the loop body may...

3 minutes read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.