Excel限定输入日期格式是yyyy-mm-dd(亲自实践)

设置单元格数据有效性规则
本文详细介绍了如何在表格中设置单元格的数据有效性规则,确保输入数据符合特定条件,通过选择'介于'而非简单的'大于'来避免误操作。

1.设置单元格格式

 

2.该单元格设置数据有效性

 

注意:

一定要选"介于",如果只是简单选择"大于",那么输入20120101这样数据,也不会报错的,而会显示######

请逐行解释下面这段代码的功能Private Sub Worksheet_Change(ByVal Target As Range) Dim wsChange As Worksheet Dim colTaskNo As Integer, colTaskCreateDate As Integer, colTaskItem As Integer, colTaskDetail As Integer Dim colDueDate As Integer, colStatus As Integer, colCompDate As Integer, colRemark As Integer Dim col_Priority_1 As Integer, col_Priority_2 As Integer Dim rowUpdate As Integer, colUpdate As Integer, taskCount As Integer Dim maxRow As Long Dim rngDataRange As Range colTaskNo = 1 colTaskCreateDate = 8 col_Priority_1 = 5 col_Priority_2 = 6 colTaskItem = 4 colTaskDetail = 15 colDueDate = 9 colStatus = 13 colCompDate = 14 colRemark = 16 rowUpdate = Target.Row colUpdate = Target.Column If rowUpdate = 1 Then Exit Sub With ThisWorkbook.Worksheets("Automation Tools") taskCount = .Cells(Rows.Count, colTaskItem).End(xlUp).Row - 1 If _ colUpdate = colTaskCreateDate Or _ colUpdate = col_Priority_1 Or _ colUpdate = col_Priority_2 Or _ colUpdate = colTaskItem Or _ colUpdate = colTaskDetail Or _ colUpdate = colDueDate Or _ colUpdate = colStatus Or _ colUpdate = colCompDate Or _ colUpdate = colRemark Then If Application.WorksheetFunction.CountA(.Range(.Cells(rowUpdate, colUpdate), .Cells(rowUpdate, colRemark))) <> 0 Then If .Cells(rowUpdate, colTaskNo) = "" Then .Cells(rowUpdate, colTaskNo) = taskCount If .Cells(rowUpdate, colTaskCreateDate) = "" Then .Cells(rowUpdate, colTaskCreateDate) = Format(Now, "yyyy-mm-dd hh:mm:ss") If .Cells(rowUpdate, colStatus) = "" Then .Cells(rowUpdate, colStatus) = "New" If .Cells(rowUpdate, colStatus) = "Completed" And .Cells(rowUpdate, colCompDate) = "" Then .Cells(rowUpdate, colCompDate) = Format(Now, "yyyy-mm-dd hh:mm:ss") End If Set rngDataRange = .Range(.Cells(2, colTaskNo), .Cells(taskCount + 1, colRemark)) With rngDataRange .VerticalAlignment = xlCenter End With .Range(.Cells(taskCount + 1, colTaskNo), .Cells(taskCount + 1, colRemark)).Interior.Color = RGB(221, 235, 247) .Cells(rowUpdate, colTaskCreateDate).NumberFormat = "yyyy-mm-dd" .Cells(rowUpdate, colDueDate).NumberFormat = "yyyy-mm-dd" .Cells(rowUpdate, colCompDate).NumberFormat = "yyyy-mm-dd" .Cells(rowUpdate, colTaskNo).HorizontalAlignment = xlCenter .Cells(rowUpdate, colTaskCreateDate).HorizontalAlignment = xlCenter .Cells(rowUpdate, colTaskItem).HorizontalAlignment = xlLeft .Cells(rowUpdate, colTaskDetail).HorizontalAlignment = xlLeft .Cells(rowUpdate, colDueDate).HorizontalAlignment = xlCenter .Cells(rowUpdate, colStatus).HorizontalAlignment = xlLeft .Cells(rowUpdate, colCompDate).HorizontalAlignment = xlCenter .Cells(rowUpdate, colRemark).HorizontalAlignment = xlLeft .Cells(rowUpdate, colTaskItem).WrapText = True With .Cells(rowUpdate, colStatus).Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:="=List!$A$2:$A$6" .IgnoreBlank = True .InCellDropdown = True .InputTitle = "" .ErrorTitle = "" .InputMessage = "" .ErrorMessage = "" .ShowInput = True .ShowError = True End With End If If Application.WorksheetFunction.CountA(.Range(.Cells(rowUpdate, colTaskItem), .Cells(rowUpdate, colTaskDetail))) = 0 Then .Rows(rowUpdate).Delete End If End If End With End Sub
最新发布
11-14
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值