EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home VBA VBA Resources VBA Date & Time Functions VBA Format Date

VBA Format Date

By Ashwani JaiswalAshwani Jaiswal

VBA Format Date

Excel VBA Format Date

In general, we have different types of Date formats that are used in all around the world but we mostly use the data in the DDMMYYYY format. For this, in VBA we have Format function which is quite popular for converting Format Date. The date or number which we feed in using the Format function converts that into the required format which we choose. Below we have the syntax of the FORMAT Function in VBA.

Syntax of FORMAT Function:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

VBA Format Date Example

Where, Format = It is the type by which we want to see the format of a date.

We know and probably have used many types of Date formats in our life and most of the formats are so commonly used such as DDMMYYYY and MMDDYYYY. Where we have seen DDMMYYYY used mostly in India and MMDDYYYY is used globally. Also, there are different separators that are used in creating a date format such as hyphen, slash, dot, brackets, etc. In this article, we will see the ways to use format the date in upcoming examples.

How to Change Date Format in VBA Excel?

Below are the examples of the excel VBA date format:

You can download this VBA Format Date Excel Template here – VBA Format Date Excel Template

Example #1

In this example, we will see a simple VBA code to format the date. We have a date in cell A1 as 25-Jun-20 as shown below.

Now we will be using the Format Date function to change the format of date in cell A1.

VBA Format Date Example 1

Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.

Insert Module

Step 2: Write the subprocedure for VBA Format Date or choose anything to define the module.

Code:

Sub VBA_FormatDate()

End Sub

VBA Format Date Example 1-1

Step 3: Choose the range cell first as A1.

Code:

Sub VBA_FormatDate()

Range("A1").

End Sub

Range Cell Example 1-2

Step 4: Then use the Number Format function as shown below.

Code:

Sub VBA_FormatDate()

Range("A1").NumberFormat =

End Sub

VBA Format Date Example 1-3

Step 5: As per the syntax of the Format function, choose the format by which we want to change the Date of the selected cell.

Code:

Sub VBA_FormatDate()

Range("A1").NumberFormat = "dd.mm.yy"

End Sub

VBA Format Date Example 1-4

Step 6: Run the code by pressing F5 or Play Button is mentioned below the menu bar. We will see the Date at cell A1 is now changed to DD.MM.YY and visible as 25.06.20.

VBA Format Date Example 1-5

Example #2

There is another way to change the Format of Date in VBA.

Step 1: For this again we would need a module and in that write the subprocedure for VBA Format Date.

Code:

Sub VBA_FormatDate1()

End Sub

Subprocedure Example 2-1

Step 2: Now using DIM declare a variable choosing the data type as a Variant. We have chosen Variant as it would allow most of the characters in it.

Code:

Sub VBA_FormatDate1()

Dim DD As Variant

End Sub

Declare Variable Example 2-2

Step 3: Put any number which we would like to see that converting into a date. Here we have chosen 43586 as shown below.

Code:

Sub VBA_FormatDate1()

Dim DD As Variant
DD = 43586

End Sub

VBA Format Date Example 2-3

Step 4: To see the output, here we will use the message box as shown below with the FORMAT function.

Code:

Sub VBA_FormatDate1()

Dim DD As Variant
DD = 43586
MsgBox Format(

End Sub

Message Box Example 2-4

Step 5: As per the syntax of the Format function, we will now use the DD variable and the format in which we want to convert the mentioned number into a date as shown below.

Code:

Sub VBA_FormatDate1()

Dim DD As Variant
DD = 43586
MsgBox Format(DD, "DD-MM-YYYY")

End Sub

VBA Format Date Example 2-5

Step 6: We will see in the message box, the Format function has converted it into 01-05-2019 as shown below.

VBA Format Date Example 2-6

Example #3

There is another way to convert a date’s format into another format as needed. For this, we will be using the same number which we have seen in the above example-1 as 44007 in cell A1.

VBA Format Date Example 3

Step 1: Open a module and in that write the subprocedure of VBA Format Date.

Code:

Sub VBA_FormatDate2()

End Sub

VBA Format Date Example 3-1

Step 2: Define a variable using DIM as Worksheet as shown below.

Code:

Sub VBA_FormatDate2()

Dim DD As Worksheet

End Sub

VBA Format Date Example 3-2

Step 3: Now using SET, choose the worksheet which we want to assign in the defined variable DD. Here, that worksheet is Sheet2.

Code:

Sub VBA_FormatDate2()

Dim DD As Worksheet
Set DD = ThisWorkbook.Sheets(2)

End Sub

VBA Format Date Example 3-4

Step 4: In a similar way as shown in example-1, we will Range cell A1 with defined variable DD and then insert the Number Format function as shown below.

Code:

Sub VBA_FormatDate2()

Dim DD As Worksheet
Set DD = ThisWorkbook.Sheets(2)
DD.Range("A1").NumberFormat =

End Sub

Range cell Example 3-5

Step 5: Now as needed, we will choose the Date format we want to see in the number at cell A1 as shown below.

Code:

Sub VBA_FormatDate2()

Dim DD As Worksheet
Set DD = ThisWorkbook.Sheets(2)
DD.Range("A1").NumberFormat = "dddd, mmmmdd, yyyy"

End Sub

VBA Format Date Example 3-5

Step 6: Once done, run the code to see the output in cell A1. We will see, as per chosen date format, cell A1 has the date as Thursday, June25, 2020.

VBA Format Date Example 3-6

Step 7: We are seeing the weekday name because as per the format we need to exclude that manually. We would insert the value as $-F800 to skip weekday.

Code:

Sub VBA_FormatDate2()

Dim DD As Worksheet
Set DD = ThisWorkbook.Sheets(2)
DD.Range("A1").NumberFormat = "[$-F800]dddd, mmmmdd, yyyy"

End Sub

VBA Format Date Example 3-7

Step 8: Now again run the code to see the required Date format. The new date format is now shown as 25 June 2020.

Date format Example 3-8

Pros of VBA Format Date

  • VBA Format Date is quite useful which helps us to see any kind of date format.
  • We can test and choose a variety of separators in VBA Format Date without any limit.

Things to Remember

  • We try to enter a date using any number, then we would get the default format which is there as per Operating System setting.
  • To see the number hidden in any date, we can first convert the date into number format and then try to use that number into the Format Date function.
  • We can only change the date format using the FORMAT function in any format we want.
  • We need to save the excel file after writing up the VBA Code in macro-enabled excel format to avoid losing the VBA code in the future.

Recommended Articles

This is a guide to the VBA Format Date. Here we discuss how to change the Date Format in VBA Excel along with practical examples and a downloadable excel template. You can also go through our other suggested articles –

  1. How to Use VBA Login?
  2. VBA Month | Examples With Excel Template
  3. How to Use Create Object Function in VBA Excel?
  4. How to Use VBA IsError Function?
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more