How to Convert Text to Date with Excel VBA – 5 Methods

The sample dataset contains text values.

Sample Data


Method 1 -Using the CDATE Function to Convert Text to Date with Excel VBA

1.1 Convert a Text String to a Date

Step 1:

  • Press Alt + F11 to open the VBA Macro.
  • Click Insert.
  • Select Module.

Effective Ways to Convert Text to Date with Excel VBA

Step 2:

  • Enter the following VBA code.
Sub Convert_Text_String_to_Date_1()
  Dim i As String
  i = "08-13"
  MsgBox i
End Sub

Effective Ways to Convert Text to Date with Excel VBA

Step 3:

  • Save the code and press F5 to run it.
  • A message box will display the text value.

Effective Ways to Convert Text to Date with Excel VBA

Step 4:

  • To convert the text, use the following VBA code.
Sub Convert_Text_String_to_Date_2()
'Declare i as a variable
  Dim i As String
  'Define the value of i
  i = "8-13"
  'Insert the CDATE function in a message box
  MsgBox CDate(i)
End Sub

Effective Ways to Convert Text to Date with Excel VBA

Step 5:

  • Save the code and press F5 to run it.

This is the output.

Effective Ways to Convert Text to Date with Excel VBA

Read More: How to Convert Date from String Using VBA


1.2 Convert a Text Serial Number to a Date

Step 1:

  • Insert a new Module.
  • Enter the following VBA code.
Sub Convert_Text_Serial_Number_to_Date_1()
'Declare i as a variable
  Dim i As String
  'Define i as a text serial number
  i = 43599
  'Insert a Message box
  MsgBox i
End Sub

Effective Ways to Convert Text to Date with Excel VBA

Step 2:

  • A message box will display the text value.

Effective Ways to Convert Text to Date with Excel VBA

Step 3:

  • To apply the CDATE function, use the following VBA code.
Sub Convert_Text_Serial_Number_to_Date_2()
'Declare i as a variable
  Dim i As String
  'Define i as a text serial number
  i = 43599
  'Apply the CDATE function and show in a message box
  MsgBox CDate(i)
End Sub

Effective Ways to Convert Text to Date with Excel VBA

Step 4:

This is the output.

Effective Ways to Convert Text to Date with Excel VBA


Method 2 – Convert Text to a Desired Date Format with Excel VBA

Step 1:

  • Insert a new Module.
  • Enter the following VBA code.
Sub Desired_Date_Format()
'Declare i as a string variable
  Dim i As String
'Declare Format_Date as a Date variable
  Dim Format_Date As Date
'Define the value of i
  i = 45566
'Apply the CDATE function
  Format_Date = CDate(i)
'Enter Format in a message box
  MsgBox Format(Format_Date, "DD-MMM-YYYY")
End Sub

Effective Ways to Convert Text to Date with Excel VBA

Step 2:

  • Save the code and press F5 to run it.

This is the output.

Effective Ways to Convert Text to Date with Excel VBA


Method 3 – Applying the DATEVALUE Function to Convert Text to Date with Excel VBA

.

Effective Ways to Convert Text to Date with Excel VBA

Step 1:

  • Enter the following VBA code.
Sub apply_DateValue_function()
Dim Convert_to_Date As Date
'Apply the DATEVALUE function
Convert_to_Date = DateValue(Range("B5").Value)
'Insert a message box to show the date
MsgBox Convert_to_Date
End Sub

Effective Ways to Convert Text to Date with Excel VBA

Step 2:

  • Save the code and press F5 to run it.

This is the output.

Effective Ways to Convert Text to Date with Excel VBA


Method 4 – Converting a Text to a Date in a Range with Excel VBA

 

Sample Data

Step 1:

  • Create a new Module,
  • Enter the following VBA code.
Sub Text_To_Date_Range()
Dim i As Long
 'Apply For loop
For i = 5 To 11
 'Loop starts from 5th row and ends at 11th row
 '3 and 2 are the column numbers
   Cells(i, 3).Value = CDate(Cells(i, 2).Value)
Next i
End Sub 

Sample Data

Step 2:

  • Save the code and press F5 to run it.

This is the output.

Sample Data


Method 5 – Converting a Text to a Date in a Specific Cell Selection

Step 1:

  • Select B6.

Sample Data

Step 2:

  • Press Alt + F11 to open a Macro.
  • Enter the following VBA code.
Sub active_cell_convert_to_date()
'Declares the variable as range
Dim myCell As Range
'Set active cell to the variable
Set myCell = ActiveCell
'Enter value in the active cell
'Apply the CDATE function
myCell.Value = CDate(myCell.Value)
End Sub

Sample Data

Step 3:

  • Save the code and press F5 to run it.

This is the output.

Sample Data


Download Practice WForkbook

Download the practice workbook.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Bhubon Costa
Bhubon Costa

Bhubon Costa, B.Sc. in Naval Architecture & Marine Engineering from Bangladesh University of Engineering & Technology, has worked with the ExcelDemy since 2021. Currently, he has been working as a reviewer. Notably, he has written over 90 articles and led several VBA content development teams. He has a great passion for the fields of data analytics and data science. His areas of expertise include Excel VBA, Power Query, Pivot Table, Power BI, MySQL, PostgreSQL, machine learning, and Python... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo