How to Convert Date from a String Using VBA – 7 Methods

 

This is the sample dataset.

VBA date from string


Method 1 – Using VBA to Convert Date to a Text String

Step 1:
 Go to Developer Tab >> Visual Basic

VBA date from string

In the Visual Basic Editor:

  • Select Insert >> Module

for a text string

A Module will be created.

for a text string

Step 2:

  • Enter the following code.
Sub datefromstring1()

Dim i As String
i = "1-10-22"
MsgBox CDate(i)

End Sub

i is declared as String and assigned to the string “1-10-22”.

CDATE will convert this string into date.

for a text string

  • Press F5

Result:
A Message Box containing the date 1/10/2022 (mm/dd/yyyy) will be displayed.

VBA date from string


Method 2 – Converting a Value to a Date Using the CDATE Function

Step 1:

  • Follow Step 1 of Method 1.
  • Enter the following code.
Sub datefromstring2()

Dim i As String
i = 44299
MsgBox CDate(i)

End Sub

i is declared as String and assigned to a value: 44299.

CDATE will convert this value into a date.

for a value

  • Press F5

Result:
A Message Box containing the date 4/13/2021 (mm/dd/yyyy) will be displayed.

for a value

Read More: How to Convert Text to Date with Excel VBA


Method 3 – Converting a Date from a String Using the DATEVALUE Function

Step 1:

  • Follow Step 1 of Method 1.
  • Enter the following code.
Sub datefromstring3()

MsgBox DateValue("1/10/2022")

End Sub

DATEVALUE will convert the text string “1/10/2022” into a date.

using DATEVALUE

  • Press F5

Result:
A Message Box containing the date 1/10/2022 (mm/dd/yyyy) will be displayed.

VBA date from string


Method 4 – Converting a Date from a String Using the FORMAT Function

Use the FORMAT function in the VBA code.

Step 1:

  • Follow Step 1 of Method 1.
  • Enter the following code.
Sub datefromstring4()

Dim i As String
i = 44299
MsgBox Format(CDate(i), "MM/DD/YYYY")

End Sub

i is declared as String and assigned to a value: 44299

CDATE will convert this string into a date and FORMAT will convert the date into the format “MM/DD/YYYY”.

using FORMAT

  • Press F5

Result:
A Message Box containing the date 4/13/2021 (mm/dd/yyyy) will be displayed.

VBA date from string


Method 5 – Converting a Range of Strings to a Date

VBA date from string

Step 1:

  • Follow Step 1 of Method 1.
  • Enter the following code.
Sub datefromstring5()

Dim i As Integer

For i = 5 To 11
    Cells(i, 4).value = Format(CDate(Cells(i, 3).value), "MM/DD/YYYY")
Next i

End Sub

i is declared as Integer and assigned to a value: 5 to 11 (within FOR loop)

The FOR loop will continue the  conversion in rows 5 to 11 in Column C and return the output values in Column DCDATE will convert the strings into a date and FORMAT will convert it into “MM/DD/YYYY”.

CDATE function

  • Press F5

Result:
Dates are displayed in the Real Date column.

CDATE function


Method 6 – Converting a Range of Strings to a Date Using the DATEVALUE Function

VBA date from string

Step 1:

  • Follow Step 1 of Method 1.
  • Enter the following code.
Sub datefromstring6()

Dim i As Integer

For i = 5 To 11
    Cells(i, 4).value = Format(DateValue(Cells(i, 3).value), "MM/DD/YYYY")
Next i

End Sub

i is declared as Integer and assigned to a value: 5 to 11 (within FOR loop)

The FOR loop will continue the conversion in rows 5 to 11 of Column C and return the output values in Column D.  DATEVALUE will convert the strings into a date and FORMAT will convert it into “MM/DD/YYYY”.

DATEVALUE function

  • Press F5

Result:
Dates are displayed in the Real Date column.

DATEVALUE function

Read More: How to Convert String to Number in Excel VBA


Method 7 – Converting a Range of Strings to Date and Time

VBA date from string

Step 1:

  • Follow Step 1 of Method 1.
  • Enter the following code.
Public Function datetimecomb(value)

Dim rslt
Dim d
Dim t

If Len(value) = 12 Then

    d = DateSerial(CInt(Left(value, 2)), CInt(Mid(value, 3, 2)), _
    CInt(Mid(value, 5, 2)))
    t = TimeSerial(CInt(Mid(value, 7, 2)), CInt(Mid(value, 9, 2)), _
    CInt(Right(value, 2)))
    rslt = CDate(CStr(d) + " " + CStr(t))

ElseIf Len(value) = 14 Then

    d = DateSerial(CInt(Left(value, 4)), CInt(Mid(value, 5, 2)), _
    CInt(Mid(value, 7, 2)))
    t = TimeSerial(CInt(Mid(value, 9, 2)), CInt(Mid(value, 11, 2)), _
    CInt(Right(value, 2)))
    rslt = CDate(CStr(d) + " " + CStr(t))

Else

    MsgBox "Invalid Format"

    End

End If

datetimecomb = rslt

End Function

This code will create a function: datetimecomb.

CINT will convert the value into an integer and CSTR will convert the value into a string. CDATE will convert the string into a date.

DATESERIAL function creates a date by extracting the year, month, and day from the string (using the LEFT and MID functions). The TIMESERIAL function returns time by extracting the hour, minute, and second from the string (using the MID and RIGHT functions).

The first IF loop will do the conversion for a 12 number digit (yymmddhhmmss). The second loop will do the conversion for a 14 number digit (yyyymmddhhmmss). Otherwise, it will return “Invalid Format”

conversion to date and time

  • Save the code and go back to the worksheet.
  • Enter the function name in D5.
=datetimecomb(C5)

conversion to date and time

  • Press ENTER and drag down the Fill Handle.

conversion to date and time

Result:
Date and time are displayed in the Real Time column.

VBA date from string


Things to Remember

  • The date format after using the CDATE function depends on the system date format.
  • Dates are stored as a serial number in Excel, so formatting is required to get the final results.

Practice Section

Practice here.

practice


Download Workbook


Further Readings

Get FREE Advanced Excel Exercises with Solutions!
Tanjima Hossain
Tanjima Hossain

TANJIMA HOSSAIN is a marine engineer who enjoys working with Excel and VBA programming. For her, programming is a handy, time-saving tool for managing data, files, and online tasks. She's skilled in Rhino3D, Maxsurf C++, MS Office, AutoCAD, and Excel & VBA, going beyond the basics. She holds a B.Sc. in Naval Architecture & Marine Engineering from BUET and is now a content developer. In this role, she creates tech-focused content centred around Excel and VBA. Apart from... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo