VBA Date Function

What is Excel VBA Date Function?

The Excel VBA Date function format is a built-in function that returns the current system date as a Date data type in VBA . The Date function does not require any arguments and can be used to retrieve the current date in Excel VBA. It returns the value which the VBA Date Function provides.

The Date function has different use cases, ranging from finding the difference between dates, returning the current time, printing the current date, and printing only the date’s month, day, or year with the Date’s help. The VBA Date function day month year format is generally accepted as arguments from the user. For example, consider the following code:

VBA Date Function Intro

It will print today’s date in the Immediate tab.

VBA Date Function Intro - Output
Key Takeaways
  • The VBA Date function creates a date value based on specified year, month, and day components.
  • It returns a Variant data type with a subtype of Date.
  • The Date function generates dates dynamically within the VBA code.
  • It can perform date-related calculations, comparisons, and manipulations.
  • Use the Date function in combination with other date functions for tasks such as calculating the difference between dates or extracting date components.

How to use Excel VBA DATE function?

To access the VBA DATE function, write the necessary code to implement it and keep it ready.

Step 1: Go to the “Developer” tab in excel in the toolbar and click the “Visual Basic” option. Now, the VBA Editor opens to add functions and Sub procedures. Then, click the “Insert” and “Module” buttons to create a new module or blank page.

Developer - Visual Basic
Insert - Module

Step 2: Consider an example where we need to find the difference in the number of days with a given date and today’s date using the VBA DATE Function reference library.

Step 3: Initialize a subroutine to print the difference between the dates.

VBA Date Function - Use - Step 3

Step 4: Define three variables, “currentDate” to hold the value for the DATE function, “futureDate” as the date given, and the “daysDifference” to hold the difference between the two dates.

VBA Date Function - Use - Step 4

Step 5: Initialize “currentDate” with the value returned by the DATE function and initialize “futureDate” by giving its value.

VBA Date Function - Use - Step 5

Step 6: Print the difference of days by subtracting “futureDate” and “currentDate” and printing it.

VBA Date Function - Use - Step 6

Here, vbNewLine enables all values to be printed in the same Debug.Print function and will print the output in a new line.

Code:

Sub DateExample()
Dim currentDate As Date
Dim futureDate As Date
Dim daysDifference As Integer
currentDate = Date
futureDate = currentDate + 7
daysDifference = futureDate – currentDate
Debug.Print “Current Date: ” & currentDate & vbNewLine & _
“Future Date: ” & futureDate & vbNewLine & _
“Difference between the dates: ” & daysDifference
End Sub

Step 6: Run the code by pressing the F5 or Run button. It will print as mentioned above.

VBA Date Function - Use - Step 6 - Output

Excel VBA – All in One Courses Bundle (35+ Hours of Video Tutorials)

If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

Examples

Here are a few examples to help understand the VBA DATE division and its use cases.

Example #1

Consider an example where you build a VBA process to check whether a given input date from the user is in the past, present, or future. The user accepts the date value using the “InputBox()” VBA function.

Step 1: Define a subroutine to check whether the given date is past, present, or future.

VBA Date Function - Example 1 - Step 1

Step 2: Declare two variables, ‘inputDate’ and ‘currentDate’, as the datatype “Date.” The variable “inputDate” is used to accept the date variable from the user, and the variable “currentDate” is used to store the Date value given by today VBA Date function.

VBA Date Function - Example 1 - Step 2

Step 3: Accept custom input from the user for “inputDate.” Initialize “currentDate” by storing the value of the Date Function.

Example 1 - Step 3

Step 4: Initialize an If-Else statement to check whether the given date is lesser than the current date; then, it will be defined as “past.” Else If not, returns “future.” Else, it returns “today.”

VBA Date Function - Example 1 - Step 4

Code:

Sub CheckDate()
Dim inputDate As Date
Dim currentDate As Date
inputDate = InputBox(“Enter a date (dd/mm/yyyy):”)
currentDate = Date
If inputDate < currentDate Then Debug.Print “The entered date is in the past.” ElseIf inputDate > currentDate Then
Debug.Print “The entered date is in the future.”
Else
Debug.Print “The entered date is today.”
End If
End Sub

Step 5: To run the code, click the green arrow button in the VBA toolbar.

VBA Date Function - Example 1 - Step 5

Step 6: Run the VBA code. The output is printed in the Immediate tab.

VBA Date Function - Example 1 - Step 6 - Msgbox
VBA Date Function - Example 1 - Output

Example #2

Suppose you want to calculate your age using Excel VBA. It can be found by subtracting your birth year and current year to return your age.

Step 1: Form a new subroutine to calculate your age.

VBA Date Function - Example 2 - Step 1

Step 2: Declare two variables, ‘dob’ and ‘age,’ with the datatype Date, where ‘dob’ is used to input your date of birth and age is the integer value to store your age.

VBA Date Function - Example 2 - Step 2

Step 3: Initialize the value for dateRange as the range of the given table (it can be changed based on the preferred range) and counter as 0.

VBA Date Function - Example 2 - Step 3

Step 4: The variable “age” is the difference between the variable “dob” and today VBA DATE function.

VBA Date Function - Example 2 - Step 4

Print “age” using the MsgBox function. It will print the age with a Message Box popup.

Code:

Sub CalculateAge()
Dim dob As Date
Dim age As Integer
dob = InputBox(“Enter your date of birth (dd/mm/yyyy):”)
age = DateDiff(“yyyy”, dob, Date)
MsgBox “Your age is: ” & age
End Sub

Step 5: To run the code, click the green arrow button in the VBA toolbar.

VBA Date Function - Example 1 - Step 5

Step 6: The resulting output is as shown.

VBA Date Function - Example 2 - Step 6
VBA Date Function - Example 2 - Step 6 - Output

Example #3

Consider an example where you print the year, month, and day of a given date separately as VBA Date Function day month year. It can be done using the VBA DatePart() Function.

Step 1: Initialize a subroutine to split and print the components of a date separately.

Example 3 - Step 1

Step 2: Initialize a Date variable “myDate” and assign it the value given by the VBA Date function.

Example 3 - Step 2

Step 3: Assign the year of the date to a variable.

Example 3 - Step 3

Step 4: Assign the month part of the given date to a variable.

Example 3 - Step 4

Step 5: Assign the day part of the given date to a variable.

Example 3 - Step 5

Step 6: Print all three variables with the MsgBox function.

Example 3 - Step 6

It will print all the values in the same MsgBox but in different lines with the help of vbNewLine.

Code:

Sub ExtractDateParts()
Dim myDate As Date
myDate = Date
Dim yearPart As Integer
yearPart = DatePart(“yyyy”, myDate)
Dim monthPart As Integer
monthPart = DatePart(“m”, myDate)
Dim dayPart As Integer
dayPart = DatePart(“d”, myDate)
MsgBox “Year: ” & yearPart & vbNewLine & _
“Month: ” & monthPart & vbNewLine & _
“Day: ” & dayPart
End Sub

Step 7: To run the code, click the green arrow button in the VBA toolbar.

Example 1 - Step 5

Step 8: After running the code, the output is shown below.

Example 3 - Step 8

Important Things To Note

  • Use the Date function to retrieve the current system date.
  • The DateAdd function can add a specified time interval to a date.
  • The DateDiff function is used to calculate the difference between two dates.
  • We use the DatePart function to extract a specific date part.
  • Use the DateSerial function to create a date value from individual year, month, and day components.
  • The Date function cannot retrieve the current system time.
  • Don’t use the Date function to perform time-based calculations with invalid date formats or inputs.
  • Be mindful of date format consistency, handling invalid dates, and considering regional settings when working with the Date function.

Frequently Asked Questions (FAQs)

1. Why is VBA Date function not working?

Incorrect syntax in the function call.
Invalid input values for year, month, or day.
The issue with variable declaration or assignment.
Date format mismatch or ambiguity.
Regional settings affecting date interpretation.
Conflict with other functions or code in the program.
VBA library or version compatibility issues.
Error handling or debugging problems.
System or software-related issues.

2. Which function returns specific part of a given date in VBA?

The VBA DatePart function can be used to return a specific part of a given VBA date function day month year. The DatePart function takes three arguments: Interval, Date, and FirstDayOfWeek. The Interval argument specifies the part of the date that you want to extract, such as year, month, day, hour, minute, or second.

3. What is the difference between VBA Date and Time Functions in Excel?

VBA Date FAQ 3

4. What is the difference between VBA Date and Time Functions in Excel?

The VBA Day function returns the day of the month for a given date value. The Day function takes one argument, which is the date value for which you want to retrieve the day of the month. The Day function returns an integer value between 1 and 31, representing the day of the month.

This has been a guide to What is VBA DATE Function. We learn how to use date function to return the current date with examples. You can learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *