ForJ9
ForJ9

Reputation: 745

ListBox Value to Date

I want to convert a value from my ListBox string value to a Date.

The string looks like: "Friday, 15 March 2015" (Excel: long date)

How can I convert it to a normal date? I tried DateValue, but it gives me an "Type mismatch" error.

Upvotes: 0

Views: 198

Answers (2)

Have you tried, the main problem is that Short Date doesn't expect the "Friday, " so you need to remove it (either with this, a regex, or split: see @MatteoNNZ solution for the split)

Dim newDate as Date
newDate = Format(Right(oldDate, 14), "Short Date")

Upvotes: 0

Matteo NNZ
Matteo NNZ

Reputation: 12665

You can reach your purpose in 2 steps; first, you need to get rid of the "day, " part of the string. Then, you can use the Format function to store your date into a Date type variable. Here's an example:

Dim myDate As Date
myString = "Sunday, 29 March 2015"
newDate = Format(Split(myString, ",")(1), "Short Date")

Upvotes: 1

Related Questions