Reputation: 257
I'm looking for a way to convert strings to Date data type in VBA. I can use CDate() function for this purpose, but it uses its own implicit algorithm for parsing strings and I would like to specify my own format to differentiate between "DD/MM/YY" and "MM/DD/YY", for example.
Please notice that unlike Visual Basic, VBA has no DateTime.TryParseExact function.
ADD: As some answers suggest, I can write my own parser. If there is no built-in solution, it will be my choice.
Upvotes: 1
Views: 3651
Reputation: 151
In this case I would avoid CDate alltogether and instead split the date string into year, month and day and use DateSerial(year, month, day). This way you have full control.
Upvotes: 1
Reputation: 1247
Do something like this...
strDate = Split(strDate,"/")
newstrDate = strDate(1) & "/" & strDate(0) & "/" & strDate(2)
strDate = CDate(newstrDate)
Upvotes: 2
Reputation: 458
Can you use the below code
Msgbox Format("24/01/2017","MMM, DD, YYYY")
This will give you output as Jan, 24, 2017
. You can use other format which is easily available when you click on the Format cells. Thanks
Upvotes: 0