我的VBA代码正在读取一个字符串日期为01-OCT-2017的文本文件。
在具有法语或比利时设置的Windows上,使用CDate()
或DateValue()
可以在大多数日期使用,但不适用于FEB-APR-MAY-JUN-JUL-AUG,其中“févr”,可以预期“AVR”,“mai”,“Juin”,“Juil”
例如,CDate("15-JUN-2017")
将返回错误
在我编写VBA函数来处理这个问题之前,我想知道是否有一些神奇的函数可以处理这个问题?
答案 0 :(得分:0)
我写了以下内容,这对法语来说似乎很有效。可以修改为涵盖所有12个月,以确保与其他语言一起使用。
Function cvtUsDt(ByVal sUsDt As String) As Date
'CDate and DateValue do not work well on US text dates like 15-FEB-2017
'on machine with some non english regional settings
'problematic months: FEB-APR-MAY-JUN-JUL-AUG-DEC
Dim sMon As String
If sUsDt Like "*FEB*" Then
sMon = Format(DateSerial(2017, 2, 10), "mmm")
sUsDt = Replace(sUsDt, "FEB", sMon)
ElseIf sUsDt Like "*APR*" Then
sMon = Format(DateSerial(2017, 4, 10), "mmm")
sUsDt = Replace(sUsDt, "APR", sMon)
ElseIf sUsDt Like "*MAY*" Then
sMon = Format(DateSerial(2017, 5, 10), "mmm")
sUsDt = Replace(sUsDt, "MAY", sMon)
ElseIf sUsDt Like "*JUN*" Then
sMon = Format(DateSerial(2017, 6, 10), "mmm")
sUsDt = Replace(sUsDt, "JUN", sMon)
ElseIf sUsDt Like "*JUL*" Then
sMon = Format(DateSerial(2017, 7, 10), "mmm")
sUsDt = Replace(sUsDt, "JUL", sMon)
ElseIf sUsDt Like "*AUG*" Then
sMon = Format(DateSerial(2017, 8, 10), "mmm")
sUsDt = Replace(sUsDt, "AUG", sMon)
ElseIf sUsDt Like "*DEC*" Then
sMon = Format(DateSerial(2017, 12, 10), "mmm")
sUsDt = Replace(sUsDt, "DEC", sMon)
End If
'in case of error, return 1/1/1900
cvtUsDt = 0
On Error Resume Next
cvtUsDt = CDate(sUsDt)
End Function