有幾個問題,你提出的aaproach:
- 將字符串轉換爲日期序列是有問題的,有不確定性,如果Excel將其解釋日期
dd.MM
或MM.dd
。由於您事先知道該格式,因此直接提取月份和年份。
\
對於圖紙名稱不是有效的字符。我用_
,替代品如你所願
Function GetMonthandYear(FileDate As String) As String
Dim dot1 As Long, dot2 As Long
Dim m As String, y As String
dot1 = InStr(FileDate, ".")
dot2 = InStr(dot1 + 1, FileDate, ".")
m = Mid$(FileDate, dot1 + 1, dot2 - dot1 - 1)
y = Mid$(FileDate, dot2 + 1, InStr(FileDate, " ") - dot2 - 1)
GetMonthandYear = Format$(DateSerial(y, m, 1), "MMMM_yyyy")
End Function
這樣稱呼它
Sub Test()
Dim FileDate As String
FileDate = "15.04.2012 16-31-18"
ActiveSheet.Name = GetMonthandYear(FileDate)
End Sub
從來沒有聽說過CDATE,但剛纔發現ToDateTime方法目前實驗 – 2012-04-15 13:35:48