Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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_Date - Fatal编程技术网

在Excel中轻松输入日期

在Excel中轻松输入日期,excel,date,Excel,Date,我使用带有时间和日期分割的时间日志。 输入1800时,我可以使用返回18:00的时间格式00:00格式化单元格。 我想知道当020521在输入后返回2021年5月2日时,日期格式是否可能类似 我注意到,当发布此消息时,反斜杠在00到: 在我的努力下,它将正确的数据返回到单元格,但在printdate=newdate时出现运行时错误13类型不匹配。如果我将Excel单元格的格式设置为最新的格式,那么它就会变得完全混乱 Option Explicit Private Sub Worksheet_C

我使用带有时间和日期分割的时间日志。 输入1800时,我可以使用返回18:00的时间格式00:00格式化单元格。 我想知道当020521在输入后返回2021年5月2日时,日期格式是否可能类似

我注意到,当发布此消息时,反斜杠在00到: 在我的努力下,它将正确的数据返回到单元格,但在printdate=newdate时出现运行时错误13类型不匹配。如果我将Excel单元格的格式设置为最新的格式,那么它就会变得完全混乱

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column = 1 Then
        Dim rawdate As String
        Dim newdate As String
        Dim printdate As Date
         If Target.Value <> "" Then
            Debug.Print Target.Value
            rawdate = Target.Value
            Debug.Print rawdate
            If Len(rawdate) = 6 Then
            newdate = Left(rawdate, 2) & "/" & Mid(rawdate, 3, 2) & "/20" & Right(rawdate, 2)
            Debug.Print newdate
            Else
            newdate = "0" & Left(rawdate, 1) & "/" & Mid(rawdate, 2, 2) & "/20" & Right(rawdate, 2)
            Debug.Print newdate

        End If
End If
printdate = newdate
Debug.Print printdate
Target.Value = printdate

End If
End Sub
选项显式
私有子工作表_更改(ByVal目标作为范围)
如果Target.Column=1,则
将原始日期设置为字符串
将newdate设置为字符串
将打印日期设置为日期
如果Target.Value为“”,则
Debug.Print Target.Value
rawdate=Target.Value
调试。打印原始日期
如果Len(rawdate)=6,则
newdate=Left(rawdate,2)&“/”和Mid(rawdate,3,2)&“/20”和Right(rawdate,2)
调试。打印新日期
其他的
newdate=“0”左(rawdate,1)和“/”中(rawdate,2,2)和“/20”右(rawdate,2)
调试。打印新日期
如果结束
如果结束
printdate=newdate
调试。打印打印日期
Target.Value=printdate
如果结束
端接头

如果您希望用户以Excel无法识别为日期的格式输入日期/时间,您可以使用VBA更改事件将输入的数据转换为日期I将当前日期和时间输入到单元格中,这些单元格使用ctrl&;开始格式化为常规格式;对于日期和cmnd&;暂时。输入后,日期单元格将被设置为日期格式,时间单元格将被自定义。。。