Table Of Contents
VBA Today Function
Today means the current date. In the worksheet, the NOW function does the same thing, which gives us the current date and time, but there is no built-in Today function in VBA. So instead, the method to get the system's current date is by using the Date function. However, unlike the NOW function, the Date function only gives us the current date.
In Excel, we have several useful functions that can help us daily. Excel made all of our lives easy at the workplace. When we say "daily," Excel also has the formula to return today's date. Not only the date, but we can also get the current date and time together. Such is the variety of excel formulas. If you are a regular user of Excel, we hope you have come across the formula “TODAY” in excel to insert the current date as shown on your working computer. But we do not have the TODAY function in VBA. So then, how do we get the TODAY date from VBA? This article will show you how to work with the TODAY date in VBA. Read on.
What is the Formula to get Today’s Date in VBA?
If there is no formula called TODAY, how do we get today's date from VBA? It is the common question everybody asks, but the solution is simple: we have a formula with a different name, i.e., the DATE function.
What Date Function Does in VBA?
The DATE function is the same as the VBA TODAY function. But this is not a volatile function unless you run the Macro or trigger the Macro.
The syntax of the DATE function does not have any arguments. So, we need to pass the function DATE. That is all.
DATE ()
Examples of Date Function in VBA
The DATE function returns the current date of the system. Using this as part of the big VBA project is very useful. To have proper knowledge about this, first, we will show you simple examples of the DATE function.
Example #1
Let us create a simple DATE function to show the current date in the message box. Then, follow the below steps to write the excel macro.
Step 1: Create a sub procedure by naming the macro.
Step 2: Declare the variable as "Date." The DATE function returns the result as a date only, so the variable data type should be "Date."
Code:
Sub Today_Example1() Dim K As String End Sub
Step 3: Assign the value to variable "k" as the DATE function.
Code:
Sub Today_Example1() Dim K As String K = Date End Sub
Step 4: Now, the value of the variable “k” in the message box in VBA.
Code:
Sub Today_Example1() Dim K As String K = Date MagBox K End Sub
Run the code. We should see the current date showing in the system.
Note: Date format could vary based on the system settings. It could be in “mm-dd-yy,” “dd-mm-yy.”
Date Function to find the Due is Today.
The Date function is more helpful in finding the due dates of EMI, credit card payments, insurance payments, etc.
Assume you are working as a loan recovery officer and have a list of customers with their due amount and due date.
In the status column, you need the result as "Due is Today" if the due date is equal to the current system date.
We can do this by using the IF condition and loops in VBA. Below is the readymade code for you to arrive at the results.
Code:
Sub Today_Example2() Dim K As Integer For K = 2 To 11 If Cells(K, 3).Value = Date Then Cells(K, 4).Value = "Due is on Today" Else Cells(K, 4).Value = "Not Today" End If Next K End Sub
It will arrive at the results in the status column.
Like this in many scenarios, we can use the DATE function to check the dates and perform some action.
You can download this VBA Today Function here. VBA Today Function Excel Template
Recommended Articles
This article has been a guide to the VBA Today function. Here, we learn how to find today's date in VBA using the Date function, practical examples, and downloadable Excel templates. Below are some useful Excel articles related to VBA: -