问题很简单: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)
答案 0 :(得分: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