Excel格式的date信息不能识别为date

我有一个生成的文件来自系统这个文件的date格式是这样的Mar 28 2016 4:55:54:380PM我在VBA中的脚本不能识别这个date或宁可Excel不认识这是一个date格式,但只有一个string。 有没有其他的方法?

你在评论中提到你只需要date:

 Sub dateTest() Dim d As Date s = "Mar 28 2016 4:55:54:380PM" s = Left(s, 11) d = DateSerial(Year(s), Month(s), Day(s)) Debug.Print d End Sub 

2016年3月28日

迭代一些数据集:

 Sub dateIteration() Dim d As Date, v As Variant Dim rng As Range Set rng = Range("A1:A10") For Each r In rng v = Left(r.Value, 11) d = DateSerial(Year(v), Month(v), Day(v)) ' Do something with d ' Print it to worksheet, maybe? r.Value = d Next r End Sub 

用最less的代码混乱来遍历不连续的范围:

 Sub helperSub() Call dateIteration(Range("A1:A10")) Call dateIteration(Range("Z1:Z10")) Call dateIteration(Range("H1:M89")) End Sub Sub dateIteration(rng As Range) Dim d As Date, v As Variant For Each r In rng v = Left(r.Value, 11) d = DateSerial(Year(v), Month(v), Day(v)) ' Do something with d ' Print it to worksheet, maybe? r.Value = d Next r End Sub 

这是一个2行代码;)

我假设范围是从A1:A20 。 请根据情况修改

 Sub Sample() [A1:A20].NumberFormat = "DD/MM/YYYY" [A1:A20] = [index(DATE(MID(A1:A20,8,4),MONTH(1&LEFT(A1:A20,3)),MID(A1:A20,5,2)),)] End Sub 

在这里输入图像描述

如果你想了解这个代码的作用,那么看看我在这里给出的解释

尝试这个

 Public Function stringtodate(mytext) As Date str1 = Split(mytext, " ") str2 = UBound(str1) If str2 > 2 Then If LCase(Left(str1(0), 3)) = "mar" Then mon = "03" End If stringtodate = str1(1) & "-" & mon & "-" & str1(2) Else 'not a valid date End If End Function 

在这里输入图像说明

不是VBA中的date格式#MM / DD / YYYY h:mm:ss.sss PM#?