简体   繁体   English

VBA cDate 不适用于 Mac excel 2011(14.7.1)

[英]VBA cDate not working on Mac excel 2011(14.7.1)

I am trying to convert a date into long using VBA script.我正在尝试使用 VBA 脚本将日期转换为 long。 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.上面的代码段在 Windows 上运行良好,但在 MAC 上给了我类型不匹配错误。 Is there any turnaround to convert a valid string in a date.在日期中转换有效字符串是否有任何周转。

Your regional settings are the root cause.您的区域设置是根本原因。

Can you try modify them?你可以尝试修改它们吗?

defaults write NSGlobalDomain AppleICUDateFormatStrings -dict 1 dd/MM/yyyy

Your code did work on my local OS X machine with Excel.您的代码确实在我的本地 OS X 机器上使用 Excel 运行。 I have different region settings, though.不过,我有不同的区域设置。 I would suggest try using the international date format ("YYYY-mm-dd").我建议尝试使用国际日期格式(“YYYY-mm-dd”)。

Sub Main
    Dim str as String
    Dim d as Variant
    str = "2016-01-01"
    d = CDate(str)
End Sub

Alternatively, you could try "Jan/1/2016".或者,您可以尝试“2016 年 1 月 1 日”。


PS: You can see your date & time settings in OS X under System Preferences -> Language & Region -> Advanced -> Dates: PS:您可以在 OS X 中的系统偏好设置 -> 语言和地区 -> 高级 -> 日期下查看您的日期和时间设置:

OS X 语言和地区日期设置

Many different answer, so I may as well throw this way of doing it it into the ring,许多不同的答案,所以我不妨把这种做法扔进戒指,

Sub test()
    Dim str As String
    Dim d As Variant
    str = "1/1/2016"
    d = CDate(Format(str, "dd/MM/yyyy"))
    Debug.Print d
End Sub

Maybe try removing the "/" before converting?也许尝试在转换之前删除“/”? I do not have a Mac to test:我没有要测试的 Mac:

Public Sub test()
    Dim str As String
    Dim d As Long 'should be Date, but your question states Long
    str = "1/1/2016"
    d = CDate(Replace(str, "/", ""))
    Debug.Print d
End Sub

Also, have you tried checking for missing references?另外,您是否尝试过检查缺失的参考文献? Date for VBA not working in Excel 2011? VBA 的日期在 Excel 2011 中不起作用?

You could use the IsDate() function to check if it is a date before converting.您可以使用 IsDate() 函数在转换之前检查它是否是日期。 Otherwise use a workaround:否则使用解决方法:

Sub test()
    Dim str as string
    Dim d as variant
    str = "1/1/2016"

    If IsDate(str) then
       d = CDate(str)
    Else
       Dim aDateParts() as String
       aDateParts() = Split(str, "/")

       ' DateSerial(Year, Month, Day)
       d = DateSerial(CInt(aDateParts(2)), CInt(aDateParts(0)), CInt(aDateParts(1)))
    End If
end sub

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 calling ConDate("12/12/2016") .我最初没有发布这个,因为我觉得它没有解决问题的根源,但是,如果你想要一个解决方法,你可以像使用CDate一样使用下面的函数,通过调用ConDate("12/12/2016")

This is is the way I approached the problem:这是我解决问题的方式:

Sub MainTest()
    Dim InputString As String, OutputDate As Date

    InputString = "01/12/2016"
    OutputDate = ConDate(InputString)

    Debug.Print OutputDate, TypeName(OutputDate)
End Sub

Function ConDate(ByRef InputString As String) As Date
    Dim Day As Long, Month As Long, year As Long
    'mmddyyyy format
    Month = CLng(Left(InputString, InStr(1, InputString, "/", vbTextCompare) - 1))
    Day = CLng(Mid(InputString, InStr(1, InputString, "/", vbTextCompare) + 1, InStrRev(InputString, "/", , vbTextCompare) - InStr(1, InputString, "/", vbTextCompare) - 1))
    year = CLng(Right(InputString, 4))
    ConDate = DateSerial(year, Month, Day)
End Function

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 "/".不想剽窃 sebifeixler 的回答我保留了我原来的左/中/右来分隔日/月/年,但我觉得使用他的拆分功能用“/”分隔日期要简洁得多。 A combination of the two would be a good workaround.两者的结合将是一个很好的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM