Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将日期从文本格式转换为日期格式部分有效_Excel_Vba_Date - Fatal编程技术网

Excel 将日期从文本格式转换为日期格式部分有效

Excel 将日期从文本格式转换为日期格式部分有效,excel,vba,date,Excel,Vba,Date,我正在尝试将日期从文本格式转换为日期格式。我在下面提供的代码适用于dd数大于12的所有日期。因此,对于dd=13到dd=31,它可以正常工作。老实说,当我与dd约会时,理解您的代码有点困难-您在VBA中处理Excel公式-不需要这样做 试试像这样的东西 Dim r As Range Set r = ThisWorkbook.Sheets(1).Range("B1") r.Value = "08/01/2017" With r Dim s As Str

我正在尝试将日期从文本格式转换为日期格式。我在下面提供的代码适用于dd数大于12的所有日期。因此,对于dd=13到dd=31,它可以正常工作。老实说,当我与dd约会时,理解您的代码有点困难-您在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
具有用户输入的版本在转换之前无需将其写入单元格

    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

完美的工作!!谢谢如果可能的话,还有一个问题。我想在另一张表中记录此日期,但另一张表中的日期格式为dd/mm/yyyy hh:mm。是否有可能仍然忽略hh:mm?嗯,很棘手,取决于确切的条件。也许,如果您可以在另一张工作表中添加一个helper列,公式为=intA2,则删除时间部分a ok!我会努力的!还有最后一件事就是你上面粘贴的代码。。它对于特定的日期非常有效,但我忘了在前面提到代码每次转换的日期是用户的输入表单,它保存为一个名为myValue的变量。。因此,我尝试添加这个变量,而不是Sheets1.RangeB1.Value,但由于myValue是一个两倍大小的变量,因此无法工作。无论如何,我已经编辑了代码,以防你能帮忙!好的,很抱歉,我修复了这个问题,它运行了,但是当dd被删除时,它看起来是mm/dd/yyyy,而不是dd/mm/yyyy。在字符串处理之前,您是否删除了将用户输入的MyValue写入范围的行?如果您将输入写入单元格并重新读取,Excel将尝试将输入转换为日期,它对此有自己的想法。请研究With命令的作用。你正在以一种绝对不正确的方式使用它,它将在将来给你带来麻烦。
    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