VBA Date to String Conversion in Excel (6 Methods)

Method 1 – Excel FORMAT Function to Convert a Date Range to Strings with VBA

Case 1.1 – Change the Whole Date Format in Range

Suppose we are using a dataset that contains some product identifiers in column B, their shipping costs in column C, and the delivery date which we are going to convert to strings. We want to change the whole date range.

Steps:

  • Insert all the dates using slashes. Excel it will automatically convert them to the date format.

Excel FORMAT Function to Convert Date Range to String in VBA

  • Right-click on the sheet and go to View Code.

Excel FORMAT Function to Convert Date Range to String in VBA

  • Copy and paste the VBA code below.

VBA Code:

Sub Date_to_String_in_Worksheet()
For i = 1 To Range("D5:D10").Rows.Count
    For j = 1 To Range("D5:D10").Columns.Count
        Range("D5:D10").Cells(i, j).Value = Application.WorksheetFunction.Text(Range("D5:D10").Cells(i, j).Value, "MM-DD-YYYY")
    Next j
Next i
End Sub

Excel FORMAT Function to Convert Date Range to String in VBA

  • To Run the code, press the F5 or play button. This will change the dates to strings.


Case 1.2 – VBA to Convert Date to Strings in a Different Column

We’ll add a column to display the converted dates.

Convert VBA Date to string in a Different Column

Steps:

  • Change the delivery date format. Instead of a slash, we will be using hyphens to change the date to strings.
  • The range D5:D10 is still in the date format. We’ll convert it into the general format.

Convert VBA Date to string in a Different Column

  • Right-click on the worksheet.
  • Go to View Code.
  • Insert this VBA code.

VBA Code:

Sub Date_to_String_in_Worksheet()
For i = 1 To Range("D5:D10").Rows.Count
    For j = 1 To Range("D5:D10").Columns.Count
        Range("E5:E10").Cells(i, j).Value = Application.WorksheetFunction.Text(Range("D5:D10").Cells(i, j).Value, "MM-DD-YYYY")
    Next j
Next i
End Sub

Excel FORMAT Function to Convert Date Range to String in VBA

  • Run the code. We will see the result in column E. All the dates are in the general format.

Convert VBA Date to string in a Different Column


Method 2 – Change a Date to a String Using the CLng Function

Steps:

  • Go to the Visual Basic editor by right-clicking on the worksheet and selecting View Code.
  • Insert the following code.

VBA Code:

Sub Date_to_Number()
Dim i As Date
i = CDate("2022-01-14")
Dim Num As Long
Num = CLng(i)
MsgBox Num
End Sub

Change Date to String Using CLng Function

  • Press F5 and run the code. We will see the date in a number.


Method 3 – VBA Date to String Conversion with the FORMAT Function

We’ll use the same sample dataset to convert the delivery dates into a string.

VBA Date to String conversion with FORMAT Function


Case 3.1 – VBA Date to String Conversion (Year, Month, Day)

Steps:

  • Go to the Developer tab and select Visual Basic. This will open the visual basic editor.

VBA Date to String Conversion (Year, Month, Day)

  • Click the Insert drop-down and select Module. This will insert a new module window.

VBA Date to String Conversion (Year, Month, Day)

We can also open the visual basic editor by right-clicking on the sheet from the sheet bar and then going to View Code.

VBA Date to String Conversion (Year, Month, Day)

  • Insert this VBA code.

VBA Code:

Sub Date_to_String()
Dim i As Date, strDate As String
i = CDate("2022-01-14")
sDate = Format(i, "YYYY-MM-DD")
MsgBox sDate
End Sub

  • Run the code or press the keyboard shortcut F5.
  • We will get a message box with the date in the YYYY-MM-DD format as shown below.


Case 3.2 – VBA Date to String Conversion (Day, Month, Year)

STEPS:

  • Use the following code:

VBA Code:

Sub Date_to_String()
Dim i As Date, strDate As String
i = CDate("2022-01-14")
sDate = Format(i, "DD MMM, YYYY")
MsgBox sDate
End Sub

VBA Date to String Conversion (Day, Month, Year)

  • Run the code by pressing the F5 or play button.
  • We will see the date in (day, month, year) format.

VBA Date to String Conversion (Year, Month, Day)


Case 3.3 – VBA Date to String Conversion (Month, Day, Year)

STEPS:

  • Insert the following code into a VBA module:

VBA Code:

Sub Date_to_String()
Dim i As Date, strDate As String
i = CDate("2022-01-14")
sDate = Format(i, "MMM DD,YYYY")
MsgBox sDate
End Sub

VBA Date to String Conversion (Month, Day, Year)

  • Press F5 or the play button to run the code. This will show the result in the message box.

VBA Date to String Conversion (Month, Day, Year)


Method 4 – VBA to Change the Present Date to a String with the NOW Function

Steps:

  • Go to View Code by right-clicking on the worksheet name.
  • Copy and paste the below VBA code into the module.

VBA Code:

Sub Present_Date_to_String()
Dim sDate As String
sDate = Format(Now(), "DD MMM,YYYY")
MsgBox sDate
End Sub

VBA to Change Present Date to String

  • Run the code by pressing F5 or clicking the play button.
  • You will see the result in the message box.

VBA to Change Present Date to String


Method 5 – Turn All Dates to Strings Using VBA in Excel

Steps:

  • Go to View Code by right-clicking on the worksheet.

Turn All Dates to String Using VBA in Excel

  • Insert this VBA code in the module.

VBA Code:

Sub Change_Date_to_String()
changetoText Selection
End Sub
Sub changetoText(target As Range)
Dim cell As Range
Dim txt As String
For Each cell In target
txt = cell.Text
cell.NumberFormat = "@"
cell.Value2 = txt
Next cell
End Sub

  • Run the code by clicking on the play button or using the keyboard shortcut F5.
  • Cells D5 to D10 are reformatted to strings.


Method 6 – Using the Number Format to Change Dates to String by Applying VBA Code

STEPS:

  • Right-click on the worksheet and go to View Code.

Turn All Dates to String Using VBA in Excel

  • Insert this code in the module.

VBA Code:

Sub Date_into_String()
Dim D As Date
D = Date
With Range("D5:D10")
.NumberFormat = "@"
.Value = Format(D, "DD MMM,YYYY")
End With
End Sub

  • Press the F5 or play button to run the code.
  • The delivery dates are converted.


Things to Remember

  • If you want to see the full month name, you have to write the month with four characters, like this (DD-MMMM-YYYY) or (MMMM-DD-YYYY) or (YYYY-MMMM-DD).

Download the Practice Workbook

Get FREE Advanced Excel Exercises with Solutions!
Sabrina Ayon
Sabrina Ayon

Sabrina Ayon, a Computer Science and Engineering graduate from United International University, has been an integral part of the ExcelDemy project for two years. She authored 150+ articles, excelling in instructing through visually engaging Excel tutorials. With a passion for teaching, Sabrina conducted sessions on Excel VBA, sharing her knowledge and insights with others. Currently holding the position of Project Manager for the ExcelDemy Visual Development Project, she oversees various aspects of the project, ensuring its smooth operation... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo