How to Get the Current Date in VBA – 3 Methods

 

Quick View:

VBA Code to Get the Current Date in VBA


Method 1 – Getting the Current Date using the Date Function of VBA

This is the code:

Current_Date=Date()

The complete code to display the current date is:

VBA Code:

Sub Get_Current_Date()

Current_Date = Date

MsgBox Current_Date

End Sub

Note: This code creates a Macro: Get_Current_Date.

VBA Code to Get the Current Date in VBA

Output:

Run this Macro, and you’ll see a Message Box displaying the current date, 11-Jan-22.

Output to Get the Current Date in VBA

Read More: How to Insert Date in Excel Formula


Method 2 – Inserting the Current Date and Time using the Now Function of VBA

This is the code:

Current_Date_and_Time = Now()

The complete code to display the current date and time is:

VBA Code:

Sub Get_Current_Date_and_Time()

Current_Date_and_Time = Now()

MsgBox Current_Date_and_Time

End Sub

Note: This code creates a Macro: Get_Current_Date_and_Time.

Output:

Run this Macro, and you’ll see a Message Box displaying the current date and time, 11-Jan-22 11:23:20 AM.

Output to Get the Current Date in VBA

Read More: How to Insert Current Date in Excel


Method 3 – Formatting the Current Date and Time using the Format Function of VBA

3.1 Format Current Date

Format the current date only.

Use this code:

=Format(Date,Format)

To display the current date in the format dd/mm/yyyy, use this code:

Current_Date = Format(Date, “dd/mm/yyyy”)

The complete VBA code is:

VBA Code:

Sub Format_Date_and_Time()

Current_Date = Format(Date, "dd/mm/yyyy")

MsgBox Current_Date

End Sub

Note: This code creates a Macro: Format_Date_and_Time.

VBA Code to Format the Current Date in VBA

Output:

Run the code. The current date is displayed in this format: dd/mm/yyyy, 11/01/2022.

3.2 Format the Current Date and Time

Use the Format function to format the current date and time: dd/mm/yyyy hh:mm:ss am/pm.

The code is:

Current_Date_and_Time = Format(Now(), "dd/mm/yyyy hh:mm:ss am/pm")

The complete VBA code is:

VBA Code:

Sub Format_Date_and_Time()

Current_Date_and_Time = Format(Now(), "dd/mm/yyyy hh:mm:ss am/pm")

MsgBox Current_Date_and_Time

End Sub

Note: This code creates a Macro: Format_Date_and_Time.

VBA Code to Format Current Date in VBA

Output:

Run the code.The current date and time will be displayed in this format: dd/mm/yyyy hh:mm:ss am/pm, 11/01/2022 12:03:45 pm.

Read more: How to Insert Dates in Excel Automatically


Download Practice Workbook

Download the practice workbook to exercise.


 

Further Readings


<< Go Back to Insert Date | Date-Time in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo