VBA Today Function
Today means the current date, in worksheet Now function does the same thing which gives us the current date and time but there is no inbuilt today function in VBA itself, the method to get the current date of the system is by using the date function and unlike the now function date function only gives us the current date.
In excel, we have several useful functions which can help us on a daily basis. Excel made all of our life easy at the workplace. When I say “daily,” excel has the formula to return today’s date as well, not only date, 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, I hope you have come across the formula called “TODAY” in excel to insert the current date as showing in your working computer. But we don’t have TODAY function in VBA, then how do we get the TODAY’s 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, then how do we get today’s date from VBA? This is the common question everybody asks, but the solution is simple we have a formula with a different name, i.e., DATE function.
What Date Function Does in VBA?
DATE is exactly 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 doesn’t have any arguments. We just need to pass the function DATE. That’s all.
Examples of Date Function in VBA
DATE function to return the current date of the system. This is very useful when we use this as part of the big VBA project. To have a fair knowledge about this first, I will show you simple examples of DATE function.
Let’s create a simple DATE function to show the current date in the message box. Follow the below steps to write the excel macro.
4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
Step 1: Create a subprocedure by naming the macro.
Step 2: Declare the variable as “Date.” DATE function returns the result as date only, so the variable data type should be “Date.”
Sub Today_Example1() Dim K As String End Sub
Step 3: Assign the value to variable “k” as DATE function.
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.
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 the context of finding the due dates of EMI, Credit Card payment, Insurance Payments, etc.…
Assume you are working as a loan recovery officer, and you 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.
This can be done by using the IF condition and loops in VBA. Below is the readymade code for you to arrive at the results.
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
This 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 kind of action.
You can download this VBA Today Function here. VBA Today Function Excel Template
This has been a guide to VBA Today function. Here we learn how to work find today’s date in VBA using Date Function along with practical examples and downloadable excel templates. Below are some useful excel articles related to VBA –