I am trying to convert a date into long using VBA script. Below is the code snippet
Sub test()
Dim str as string
Dim d as variant
str="1/1/2016"
d=cdate(str)
end sub
The above snippet runs fine on windows but is giving me type mismatch error on MAC. Is there any turnaround to convert a valid string in a date.
Your code did work on my local OS X machine with Excel. I have different region settings, though. I would suggest try using the international date format ("YYYY-mm-dd").
Alternatively, you could try "Jan/1/2016".
P.S.: You can see your date & time settings in OS X under System Preferences -> Language & Region -> Advanced -> Dates:
You could use the IsDate() function to check if it is a date before converting. Otherwise use a workaround:
Your regional settings are the root cause.
Can you try modify them?
Maybe try removing the "/" before converting? I do not have a Mac to test:
Also, have you tried checking for missing references? Date for VBA not working in Excel 2011?
I didn't post this originally, because I felt it didn't address the root of the problem, however, if you want a workaround, you can use the below function in a similar way as you would use a
CDate
, by callingConDate("12/12/2016")
.This is is the way I approached the problem:
Not wanting to plagerise sebifeixler's answer I kept my original left/mid/right to separate the day/month/year, but I feel it's much neater to use his split function to separate the date by "/". A combination of the two would be a good workaround.
Many different answer, so I may as well throw this way of doing it it into the ring,