VBA Get Today's Date

Introduction

In this article we'll discussing how to use VBA to get today's date. Getting the current date is a functionality that is often needed in your worksheet and we'll show you how to do it. We will be using the Date function for us to get the current date.

Date Function

Although there is no Today function with VBA you can accomplish the same thing with the Date function. Luckily is also has a very simple syntax.

Steps in creating a date function:

  1. Go to developers tab.
  2. Click on view code. VBA window will appear after clicking the View tab.
  3. Now you can start entering your code by inputting your sub procedure code.
  4. In between your subprocedure code, place your variables.

See below code:

    Sub example1()

        Dim i As String
        i = Date

    MsgBox i

    End Sub

Code explanation:

  • Dim i As String - this will let VBA know that we are setting the string as i
  • i = Date - this is our variable
  • MsgBox i - this will indicate that the return should be shown in the message box.

And the result will be:

Screenshot of the code on how to get the current date

Another macro code we can use to get the current date.

Code:

```Sub example2()
<pre><code>Dim CD As Date
CD = Date

MsgBox "The date today is:" & CD
</code></pre>
End Sub
<pre><code>
Code explanation:

<ul>
<li><code>Dim CD As Date</code> - this is defined as our integer</p></li>
<li><p><code>CD = Date</code> - this is the value of our integer</p></li>
<li><code>MsgBox "The date today is:" &amp; CD</code> this will instruct VBA to give us a result on our message box.</li>
</ul>

<p>And the result will be:

<img src="https://i.gyazo.com/789f3d3c300c93eaeb7ac8ac36745813.jpg" alt="Screenshot of the other code on how to get the current date and the result" />

<h3>Using the Date function to check due dates</h3>

See the data below:

You can see a variety of names with due dates beside it. Let's find out whose name already have their due dates today.

<img src="https://i.gyazo.com/5046aa2c851c25afb8bf7673e109db1d.jpg" alt="Screenshot of reference data for finding due dates" />

Code:

</code></pre>
<pre><code>Sub example2()

<pre><code> Dim i As Integer
</code></pre>

For i = 2 To 16

<pre><code>If Cells(i, 2).Value = Date Then
Cells(i, 4).Value = "Due is on Today"
Else
Cells(i, 4).Value = "Not Today"
End If
</code></pre>

Next i

End Sub
</code></pre>
```

Code explanation:

  • Dim i As Integer - this is to set our integer
  • For i = 2 To 16 - this is to let VBA know the range of our integer
  • If Cells(i, 2).Value - this is for VBA to know the range of cells to check if its today's due date
  • Cells(i, 4).Value = "Due is on Today" - this is the range of cells wherein the status will be shown, and the texts should be Due is on Today for the due dates.
  • Cells(i, 4).Value = "Not Today" - this is the range of cells wherein the status will be shown, and the texts should be Not Today for the due dates.

Note: we have used loops method in searching for today's due date.

And the result will be:

Screenshot of the result for finding today's due date

Conclusion

Remember that although there is no explicit Today function in VBA you can easily accomplish the same thing using Date. We have showed you a couple examples how to use the Date functionality to get the current date, we hope you were able to follow along and incorporate some of the same logic into your worksheets. Thanks for joining us!

Published: 
July 7, 2020
Author:
500 Rockets
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram