从文本格式到date格式的date转换部分工作

我正在尝试将date从文本格式转换为date格式。 我在下面提供的代码对于所有具有大于12的数字的date都适用。因此,对于dd = 13到dd = 31,它工作正常。 当我与dd <13的date,那么它只是不工作。 我在下面提供的date是代码不能工作的date的一个例子。 它使单元格B1空着。 此外,我的系统设置设置为dd / mm / yyyy,因此可以与系统配合使用。 任何人都知道为什么它不工作?

Sheets("1").Select Dim myValue As Variant myValue = InputBox("Simi, type the date of the shipments that you want to create the report for", "Date", 1) Sheets("1").Range("B1").Value = myValue With Sheets("1").Range("B1") Dim t() As String t = Split(myValue, "/") ' Split into pieces If UBound(t) = 2 Then ' If 3 elements found: myValue = DateSerial(t(2), t(1), t(0)) ' Convert to Date End If End With 

说实话,有一点难以理解你的代码 – 你在某种程度上处理VBA中的Excel公式 – 不需要这个。

尝试类似

  Dim r As Range Set r = ThisWorkbook.Sheets(1).Range("B1") r.Value = "08/01/2017" With r Dim s As String, t() As String s = r.Value ' Read cell content into string t = Split(s, "/") ' Split into pieces If UBound(t) = 2 Then ' If 3 elements found: r.Value = DateSerial(t(2), t(1), t(0)) ' Convert to Date End If End With 

带有用户input的版本(在转换之前不需要写入单元格)

  Dim myValue As Variant myValue = InputBox("Simi, type the date of the shipments that you want to create the report for", "Date", 1) Dim r As Range Set r = ThisWorkbook.Sheets(1).Range("B1") With r Dim s As String, t() As String s = myValue ' Use user input t = Split(s, "/") ' Split into pieces If UBound(t) = 2 Then ' If 3 elements found: r.Value = DateSerial(t(2), t(1), t(0)) ' Convert to Date End If End With