Python account
Python account

Reputation: 437

VBA Convert String to Date using periods as date seperators

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")

Upvotes: 0

Views: 858

Answers (1)

braX
braX

Reputation: 11735

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.

Upvotes: 1

Related Questions