Home > Software engineering >  VBA Convert date to string
VBA Convert date to string

Time:11-20

I have tried the following but not working,

Dim my_date_string As String
Dim my_date_date As Date

my_date_string = "22.10.2020"
my_date_date = CDate(my_date_string)

Debug.Print my_date_string
Debug.Print my_date_date

Also tried with,

my_date_date = Format(my_date_string, "DD.MM.YYYY")

CodePudding user response:

CDate does not understand periods as separators.

If you need them to be periods in your String variable for some reason, just replace them like this:

my_date_date = CDate(Replace(my_date_string, ".", "/"))

If your variable does not have periods in it, the Replace function will simply do nothing.

  •  Tags:  
  • vba
  • Related