ไปยังเนื้อหาหลัก

จะอนุญาตเฉพาะรูปแบบวันที่ในเซลล์เฉพาะได้อย่างไร

คุณจะ จำกัด รายการเซลล์เพื่ออนุญาตให้ป้อนรูปแบบวันที่ในแผ่นงาน Excel ได้อย่างไร ในบทความนี้ฉันจะพูดถึงเทคนิคที่น่าสนใจในการจัดการกับงานนี้

อนุญาตเฉพาะรูปแบบวันที่ในเซลล์เฉพาะที่มีฟังก์ชันการตรวจสอบข้อมูล

อนุญาตเฉพาะรูปแบบวันที่ในเซลล์เฉพาะที่มีรหัส VBA


ลูกศรสีฟ้าฟองขวา อนุญาตเฉพาะรูปแบบวันที่ในเซลล์เฉพาะที่มีฟังก์ชันการตรวจสอบข้อมูล

ใน Excel ไฟล์ การตรวจสอบข้อมูล เป็นฟังก์ชันที่มีประสิทธิภาพซึ่งสามารถช่วยคุณในการสร้างรายการแบบหล่นลงเพื่อป้องกันรายการที่ซ้ำกัน ฯลฯ นอกจากนี้ยังสามารถช่วยคุณป้องกันการป้อนรูปแบบข้อมูลอื่น แต่จะมีเพียงรูปแบบวันที่ในเซลล์ที่ระบุเท่านั้น โปรดทำตามขั้นตอนต่อไปนี้:

1. คลิก ข้อมูล > การตรวจสอบข้อมูล > การตรวจสอบข้อมูลดูภาพหน้าจอ:

doc อนุญาตวันที่ 1

2. ใน การตรวจสอบข้อมูล คลิกตกลง การตั้งค่า แล้วเลือก ประเพณี จาก อนุญาต รายการแบบหล่นลงจากนั้นป้อนสูตรนี้: = AND (ISNUMBER (B2), LEFT (CELL ("รูปแบบ", B2), 1) = "D") เข้าไปใน สูตร กล่องข้อความดูภาพหน้าจอ:

doc อนุญาตวันที่ 2

หมายเหตุ: ในสูตรข้างต้น B2 เป็นเซลล์แรกของคอลัมน์ที่คุณต้องการ จำกัด รูปแบบข้อมูล

doc อนุญาตวันที่ 3

3. จากนั้นไปที่คลิก การแจ้งเตือนข้อผิดพลาด ในกล่องโต้ตอบโปรดดำเนินการดังต่อไปนี้:

(1. ) ตรวจสอบ แสดงการแจ้งเตือนข้อผิดพลาดหลังจากป้อนข้อมูลที่ไม่ถูกต้อง ตัวเลือก;

(2. ) ภายใต้ สไตล์ แบบเลื่อนลงโปรดเลือก หยุด ตัวเลือก;

(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 AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 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