【问题标题】:CDate format recognition issueCDate 格式识别问题
【发布时间】:2015-11-17 23:48:48
【问题描述】:

问题很简单:CDate("Oct 01, 2015") 给了我Type mismatch 错误,因为我正在使用非英语设置。 CDate("Okt 01, 2015") 工作得很好。日期格式为Mon DD, YYYY

如何在不更改语言/位置设置或进行一些字符串操作的情况下让 Excel 识别格式?

使用数组来交换月份名称看起来不够花哨。

month_name(0, 0) = "Jan"
month_name(0, 1) = "Jan"
month_name(1, 0) = "Feb"
month_name(1, 1) = "Feb"
month_name(2, 0) = "Mar"
month_name(2, 1) = "Már"
month_name(3, 0) = "Apr"
month_name(3, 1) = "Ápr"
month_name(4, 0) = "May"
month_name(4, 1) = "Máj"
month_name(5, 0) = "Jun"
month_name(5, 1) = "Jún"
month_name(6, 0) = "Jul"
month_name(6, 1) = "Júl"
month_name(7, 0) = "Aug"
month_name(7, 1) = "Aug"
month_name(8, 0) = "Sep"
month_name(8, 1) = "Szep"
month_name(9, 0) = "Oct"
month_name(9, 1) = "Okt"
month_name(10, 0) = "Nov"
month_name(10, 1) = "Nov"
month_name(11, 0) = "Dec"
month_name(11, 1) = "Dec"

str = "Oct 01, 2015"

For counter = 0 To 11
    If Left(str, 3) = month_name(counter, 0) Then
        str = month_name(counter, 1) & Right(str, Len(str) - 3)
        Exit For
    End If
Next counter

day_date = CDate(str)

【问题讨论】:

    标签: excel date vba


    【解决方案1】:

    我建议将它创建为一个函数,以便任何其他程序都可以使用它

    Option Base 1
    
    Function Date_Translation(sDate As String) As Date
    Dim aMth1 As Variant, aMth2 As Variant
    aMth1 = [{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov", "Dec"}]
    aMth2 = [{"Jan","Feb","Már","Ápr","Máj","Jún","Júl","Aug","Szep","Okt","Nov", "Dec"}]
    Dim b As Byte
        For b = 1 To 12
            sDate = Replace(sDate, aMth1(b), aMth2(b))
        Next
        Date_Translation = CDate(sDate)
    End Function
    

    【讨论】:

    • 很高兴它对您有所帮助,请确认答案并帮助我们保持网站更新并完成问题...
    • 准确地说,问题是关于“在不更改语言/位置设置或进行一些字符串操作的情况下识别格式”。据此,我虽然很感激,但仍有疑问。在这种情况下,你们会选择绿色勾号吗?
    • 有字符串操作,虽然你已经有了这个想法,但它肯定得到了改进,不仅仅是通过更好的数组设置,循环被缩短并设置为一个函数。然而,这取决于你,我更关心的是所有那些仍然悬而未决的问题,只是因为它被问到它是不可能的,即使提出了好的替代方案。想想当我们重新审视一个仍在等待“正确答案”的问题,然后我们发现提供的答案是好的,对此无能为力,但问题仍未得到解答……
    • 说得好!再次感谢!
    猜你喜欢
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多