How to Format Date with VBA in Excel (4 Methods)

In the dataset, the same dates are in Columns B and C. We will format the date in Column C.


Method 1 – Using VBA to Format Dates

Steps:

  • Press Alt + F11, or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

  • In the pop-up code window, click Insert -> Module from the menu bar.

  • Enter the following code:
Sub DateFormat()
    Range("C5").NumberFormat = "dddd-mmmm-yyyy"
   'This will format the date to "Tuesday-January-2022"
End Sub

VBA to Format Date from One Type to Another in Excel

  • Press F5, or select run from the menu bar Run -> Run Sub/UserForm. (You can also click on the small Play icon in the sub-menu bar to run the macro.)

This code will format the date “11-01-22” to “Tuesday-January-2022”.

You can also convert this date format into many other formats. Just follow the code below to transform the date into your required format.

Sub FormatDate()
'Original Date is "11-01-22"
   Range("C5").NumberFormat = "dd-mm-yyy"
   'This will format the date to "11-01-2022"
   Range("C6").NumberFormat = "ddd-mm-yyy"
   'This will format the date to "Tue-11-2022"
   Range("C7").NumberFormat = "dddd-mm-yyy"
   'This will format the date to "Tuesday-11-2022"
   Range("C8").NumberFormat = "dd-mmm-yyy"
   'This will format the date to "11-Jan-2022"
   Range("C9").NumberFormat = "dd-mmmm-yyy"
   'This will format the date to "11-January-2022"
   Range("C10").NumberFormat = "dd-mm-yy"
   'This will format the date to "11-01-22"
   Range("C11").NumberFormat = "ddd mmm yyyy"
   'This will format the date to "Tue Jan 2022"
   Range("C12").NumberFormat = "dddd mmmm yyyy"
   'This will format the date to "Tuesday January 2022"
End Sub

VBA code to Format Date from One Type to Another in Excel

Overview

Overview of VBA to Format Date from One Type to Another in Excel

Read more: Now and Format Functions in Excel VBA


Method 2 – Embedding VBA to Convert Date Using the FORMAT Function

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Enter the following code:
Sub Format_Date()
  Dim iDate As Variant
  iDate = 44572 'Excel serial number of date "11 January 2022"
  MsgBox Format(iDate, "DD-MMMM-YYYY")
End Sub

VBA to Format Date with FORMAT Function in Excel

You will get “11 January 2022” in the message box.

Read more: How to Use VBA DateValue Function in Excel


Method 3 – Using VBA to Transform Date-Based to a Specific Part

If you want to format a specific part of the date, for instance, only the day/month/year,

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Enter the following code:
Sub Date_Format()
    Range("C5").NumberFormat = "mmmm"
   'This will format the date to "January"
End Sub

VBA to Convert Date Based on a Specific Part in Excel

The “mmmm” in this code means the long form of the month name.

You can Implement this code to format and extract any specific part you want from the date.

Sub FormatDateValue()
'Original Date is "11-01-22"
   Range("C5").NumberFormat = "dd"
   'This will format the date to "11"
   Range("C6").NumberFormat = "ddd"
   'This will format the date to "Tue"
   Range("C7").NumberFormat = "dddd"
   'This will format the date to "Tuesday"
   Range("C8").NumberFormat = "m"
   'This will format the date to "1"
   Range("C9").NumberFormat = "mm"
   'This will format the date to "01"
   Range("C10").NumberFormat = "mmm"
   'This will format the date to "Jan"
   Range("C11").NumberFormat = "yy"
   'This will format the date to "22"
   Range("C12").NumberFormat = "yyyy"
   'This will format the date to "2022"
   Range("C13").NumberFormat = "dd-mm"
   'This will format the date to "11-01"
   Range("C14").NumberFormat = "mm-yyy"
   'This will format the date to "01-2022"
   Range("C15").NumberFormat = "mmm-yyy"
   'This will format the date to "Jan-2022"
   Range("C16").NumberFormat = "dd-yy"
   'This will format the date to "11-22"
   Range("C17").NumberFormat = "ddd yyyy"
   'This will format the date to "Tue 2022"
   Range("C18").NumberFormat = "dddd-yyyy"
   'This will format the date to "Tuesday-2022"
   Range("C19").NumberFormat = "dd-mmm"
   'This will format the date to "11-Jan"
   Range("C20").NumberFormat = "dddd-mmmm"
   'This will format the date to "Tuesday-January"
End Sub

Overview

Overview of VBA to Convert Date Based on a Specific Part in Excel


Method 4 – Inserting VBA to Format Date in a Specific Worksheet 

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Enter the following code:
Sub Format_Date()
Dim iSheet As Worksheet
Set iSheet = ThisWorkbook.Sheets("Example") 'Set the worksheet named "Example"
iSheet.Range("C5").NumberFormat = "dddd, mmmmdd, yyyy"
End Sub

VBA to Format Date in a Specific Worksheet in Excel

Look at the third line of the code, where we first set the “Example” worksheet and then format the date of that particular Excel sheet.


Download the Workbook

You can download the free practice Excel workbook from here.


Further Readings

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo