VBA - convert to date

InformatikBabo picture InformatikBabo · Nov 6, 2013 · Viewed 95.5k times · Source

I have one more time a problem:

I want to convert from Strings to dates in VBA

The Strings look like: YYYY-DD-MM

The date should be like: DD.MM.YYYY

I know, normally you do this with the method cdate(), but it doesn't work here. I think it's because the structure of the string is bad to convert.

thanks for your help

InformatikBabo

Answer

user2140173 picture user2140173 · Nov 6, 2013
Sub Main()

    Dim strDate As String
    strDate = "2013-06-11"

    Debug.Print "Original Date: ", strDate
    Debug.Print "CDate() Conversion: ", CDate(strDate)
    Debug.Print "Format() as String: ", Format(strDate, "DD.MM.YYYY")

End Sub

and the Immediate Window shows

enter image description here