跳到主要内容

如何在特定单元格中仅允许日期格式?

您如何将单元格列表限制为仅允许在Excel工作表中输入日期格式? 在本文中,我将讨论处理此工作的一些有趣技巧。

具有数据验证功能的特定单元格仅允许日期格式

使用VBA代码在特定单元格中仅允许日期格式


箭头蓝色右气泡 具有数据验证功能的特定单元格仅允许日期格式

在Excel中, 数据验证 是一项功能强大的功能,可以帮助您创建下拉列表,防止重复输入等。它还可以帮助您防止在特定单元格中输入其他数据格式,而只能输入日期格式。 请执行以下步骤:

1。 点击 时间 > 数据验证 > 数据验证,请参见屏幕截图:

doc允许日期1

2。 在 数据验证 对话框,单击 个人设置 标签,然后选择 定制版 来自 下拉列表,然后输入以下公式: = AND(ISNUMBER(B2),LEFT(CELL(“ format”,B2),1)=“ D”)公式 文本框,请参见屏幕截图:

doc允许日期2

备注:在以上公式中, B2 是您要限制数据格式的列的第一个单元格。

doc允许日期3

3. 然后继续点击 错误警报 对话框中的“标签”,请执行以下操作:

(1.)检查 输入无效数据后显示错误警报 选项;

(2.)在 样式 下拉菜单,请选择 Stop 停止 选项;

(3.)键入 标题错误信息 要在提示框中显示的内容。

4。 完成设置后,单击 OK 按钮,现在,当您输入非实时日期格式的数据时,将弹出一个提示框提醒您,如下所示的屏幕截图:

doc允许日期4


箭头蓝色右气泡 使用VBA代码在特定单元格中仅允许日期格式

第二种方法,我将为您介绍一个VBA代码。

1。 激活您要使用的工作表。

2。 然后右键单击工作表标签,然后选择 查看代码 从上下文菜单中,查看屏幕截图:

doc允许日期5

3。 在显示 Microsoft Visual Basic应用程序 窗口,将以下代码复制并粘贴到 模块,请参见屏幕截图:

VBA代码:仅允许在特定的单元格中输入日期格式:

Private Sub Worksheet_Change(ByVal Target As Range)
'updateby Extendoffice
Set w = ActiveSheet.Range("B2:B12")
For Each c In w
If c.Value <> "" And Not IsDate(c) Then
c.ClearContents
MsgBox "Only a date format is permitted in this cell."
End If
Next c
End Sub

doc允许日期6

4。 然后保存广告以关闭代码,现在,当您输入范围为B2:B12的非日期格式的值时,将弹出提示框提醒您,同时将清除单元格值,请参见屏幕截图:

doc允许日期7

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!

 

Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Buenos dias

Estoy utilizando la formula en la validación de datos pero no me deja ingresar ningún formato de fecha, me puede explicar por favor

Muchas gracias
This comment was minimized by the moderator on the site
Hi I want to restrict the Date Format in DD.MM.YYYY in excel
May please suggest
This comment was minimized by the moderator on the site
Hello, Ramesh
To restrict the Date Format in DD.MM.YYYY, you can apply the below code:
Note: Please change the cell reference B2:B12 to your own list of cell.
Private Sub Worksheet_Change(ByVal Target As Range)
'updateby Extendoffice
    On Error Resume Next
    
    Dim Bool As Boolean
    Dim Rg As Range
    Dim xRegEx As Object
    
    Bool = False

    Set Rg = Application.Intersect(Target, ActiveSheet.Range("B2:B12"))
    If Rg Is Nothing Then Exit Sub

    Set xRegEx = CreateObject("VBSCRIPT.REGEXP")
    With xRegEx
        .Pattern = "^[0-3]\d\.[0-1][0-2]\.\d{4}$"
        .Global = True
        .IgnoreCase = True
    End With
    
    Application.EnableEvents = False
    For Each c In Target
        If c.Value <> "" Then
            If Not xRegEx.test(c.Text) Then
                c.ClearContents
                Bool = True
            End If
        End If
    Next c
    Application.EnableEvents = True
    If Bool Then
        MsgBox "Error date format."
    End If
End Sub


Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Hello,Sorry for refreshing, the code works very nice, thank you. I just want to add one more check if its possible.Is it possible to check cell on the left if it contain certain value. Basically what I need is: if cell on the left contain N then that cell in range have to be input as a date.If cell on the left is Y then that cell needs to stay blank.
This comment was minimized by the moderator on the site
How can I oblige to enter a time? (hh:mm)Private Sub Worksheet_Change(ByVal Target As Range)
'updateby Extendoffice 20150530
Set w = ActiveSheet.Range("B2:B12")
For Each c In w
If c.Value <> "" And Not IsDate(c) Then <----------------------------And Not isTime(c) doesn't work!
c.ClearContents
MsgBox "Only a date format is permitted in this cell."
End If
Next c
End Sub
This comment was minimized by the moderator on the site
Hello, Roberto,

To only allow time format to be entered, you should apply the below code:



Private Sub Worksheet_Change(ByVal Target As Range)

'updateby Extendoffice

Dim xArr As Variant

Dim xF As Integer

Dim xB As Boolean

Dim xWRg, xCRg As Range

Set xWRg = ActiveSheet.Range("B2:B20")

For Each xCRg In xWRg

xArr = Split(xCRg.Value, ":")

xB = False

If UBound(xArr) <= 2 Then

For xF = LBound(xArr) To UBound(xArr)

If IsNumeric(xArr(xF)) Then

If xF = 0 Then

If (xArr(xF) > 0) And (xArr(xF) < 24) Then

Else

xB = True

GoTo BTime

End If

Else

If (xArr(xF) > 0) And (xArr(xF) < 60) Then

Else

xB = True

GoTo BTime

End If

End If

Else

xB = True

GoTo BTime

End If

Next

Else

MsgBox "Only a time format is permitted in this cell."

xCRg.ClearContents

End If

BTime:

If xB Then

MsgBox "Only a time format is permitted in this cell."

xCRg.ClearContents

End If

Next xCRg

End Sub



Please try it, thank you!
This comment was minimized by the moderator on the site
Thank you so much, for the VBA code to be applied to multiple columns can you please provide the formula?.. I tried but ended up getting a 405 error!
This comment was minimized by the moderator on the site
Hello, Kevin,
To make the code applied for multiple ranges, you just need to add the cell references into the code as below:

Private Sub Worksheet_Change(ByVal Target As Range)
Set w = ActiveSheet.Range("B2:B12,A1:A10,C5:C20")
For Each c In w
If c.Value <> "" And Not IsDate(c) Then
c.ClearContents
MsgBox "Only a date format is permitted in this cell."
End If
Next c
End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Thanks a lot @skyyang, really appreciate your help :)
This comment was minimized by the moderator on the site
how to add a date validation for date format "DD-mmm-yyy" (15-Dec-2018) like this
This comment was minimized by the moderator on the site
which date format is this function allowed. It is not working for some dates
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations