Loncat ke daftar isi utama

Bagaimana cara mengizinkan hanya format tanggal di sel tertentu?

Pengarang: Xiaoyang Terakhir Dimodifikasi: 2021-01-07

Bagaimana Anda bisa membatasi daftar sel untuk hanya mengizinkan memasukkan format tanggal di lembar kerja Excel? Pada artikel ini, saya akan membahas beberapa trik menarik untuk menangani pekerjaan ini.

Izinkan hanya format tanggal dalam sel tertentu dengan fungsi Validasi Data

Izinkan hanya format tanggal dalam sel tertentu dengan kode VBA


panah gelembung kanan biru Izinkan hanya format tanggal dalam sel tertentu dengan fungsi Validasi Data

Di Excel, file Validasi Data adalah fungsi yang kuat yang dapat membantu Anda membuat daftar drop-down, untuk mencegah entri duplikat, dll. Ini juga dapat membantu Anda untuk mencegah memasukkan format data lain tetapi hanya format tanggal di sel tertentu. Silakan lakukan dengan langkah-langkah berikut:

1. Klik Data > Validasi Data > Validasi Data, lihat tangkapan layar:

doc memungkinkan tanggal 1

2. di Validasi Data kotak dialog, klik Settings tab, lalu pilih Kustom dari mengizinkan daftar drop-down, lalu masukkan rumus ini: = AND (ISNUMBER (B2), LEFT (SEL ("format", B2), 1) = "D") ke dalam Rumus kotak teks, lihat tangkapan layar:

doc memungkinkan tanggal 2

Note: Dalam rumus di atas, B2 adalah sel pertama dari kolom yang ingin Anda batasi format datanya.

doc memungkinkan tanggal 3

3. Lalu lanjutkan dengan mengklik Peringatan Kesalahan tab di kotak dialog, lakukan operasi berikut:

(1.) Periksa Tampilkan peringatan kesalahan setelah data yang tidak valid dimasukkan pilihan;

(2.) Di bawah Gaya drop down, silakan pilih berhenti pilihan;

(3.) Ketik Judul dan Error message yang ingin Anda tampilkan di kotak prompt.

4. Setelah menyelesaikan pengaturan, klik OK , dan sekarang, ketika Anda memasukkan data yang bukan format tanggal sebenarnya, sebuah kotak prompt akan muncul untuk mengingatkan Anda seperti gambar berikut yang ditampilkan:

doc memungkinkan tanggal 4


panah gelembung kanan biru Izinkan hanya format tanggal dalam sel tertentu dengan kode VBA

Metode kedua, saya akan memperkenalkan kode VBA untuk Anda.

1. Aktifkan lembar kerja yang ingin Anda gunakan.

2. Kemudian klik kanan tab lembar, dan pilih Lihat kode dari menu konteks, lihat tangkapan layar:

doc memungkinkan tanggal 5

3. Di layar Microsoft Visual Basic untuk Aplikasi jendela, salin dan tempel kode di bawah ini ke dalam Modul, lihat tangkapan layar:

Kode VBA: Hanya izinkan format tanggal masuk dalam sel tertentu:

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 memungkinkan tanggal 6

4. Kemudian simpan iklan tutup kodenya, sekarang, ketika Anda memasukkan nilai yang bukan format tanggal dalam rentang B2: B12, kotak prompt akan muncul untuk mengingatkan Anda, dan nilai sel akan dihapus pada saat yang sama, lihat tangkapan layar:

doc memungkinkan tanggal 7

Alat Produktivitas Kantor Terbaik

馃 Kutools AI Ajudan: Merevolusi analisis data berdasarkan: Eksekusi Cerdas   |  Hasilkan Kode  |  Buat Rumus Khusus  |  Analisis Data dan Hasilkan Grafik  |  Aktifkan Fungsi Kutools...
Fitur Populer: Temukan, Sorot, atau Identifikasi Duplikat   |  Hapus Baris Kosong   |  Gabungkan Kolom atau Sel tanpa Kehilangan Data   |   Putaran tanpa Formula ...
Pencarian Super: VLookup Beberapa Kriteria    VLookup Nilai Berganda  |   VLookup di Beberapa Lembar   |   Pencarian Fuzzy ....
Daftar Drop-down Lanjutan: Buat Daftar Drop Down dengan Cepat   |  Daftar Drop Down yang Bergantung   |  Multi-pilih Drop Down List ....
Manajer Kolom: Tambahkan Jumlah Kolom Tertentu  |  Pindahkan Kolom  |  Alihkan Status Visibilitas Kolom Tersembunyi  |  Bandingkan Rentang & Kolom ...
Fitur Unggulan: Fokus Kisi   |  Tampilan Desain   |   Bar Formula Besar    Manajer Buku Kerja & Lembar   |  Perpustakaan Sumberdaya (Teks otomatis)   |  Pemetik tanggal   |  Gabungkan Lembar Kerja   |  Enkripsi/Dekripsi Sel    Kirim Email berdasarkan Daftar   |  Filter Super   |   Filter Khusus (filter tebal/miring/coret...) ...
15 Perangkat Teratas12 Teks Tools (Tambahkan Teks, Hapus Karakter, ...)   |   50 + Grafik jenis (Gantt Chart, ...)   |   40+ Praktis Rumus (Hitung usia berdasarkan ulang tahun, ...)   |   19 Insersi Tools (Masukkan Kode QR, Sisipkan Gambar dari Jalur, ...)   |   12 Konversi Tools (Angka ke Kata, Konversi Mata Uang, ...)   |   7 Gabungkan & Pisahkan Tools (Lanjutan Gabungkan Baris, Pisahkan Sel, ...)   |   ... dan banyak lagi

Tingkatkan Keterampilan Excel Anda dengan Kutools for Excel, dan Rasakan Efisiensi yang Belum Pernah Ada Sebelumnya. Kutools for Excel Menawarkan Lebih dari 300 Fitur Lanjutan untuk Meningkatkan Produktivitas dan Menghemat Waktu.  Klik Di Sini untuk Mendapatkan Fitur yang Paling Anda Butuhkan...

Deskripsi Produk


Tab Office Membawa antarmuka Tab ke Office, dan Membuat Pekerjaan Anda Jauh Lebih Mudah

  • Aktifkan pengeditan dan pembacaan tab di Word, Excel, PowerPoint, Publisher, Access, Visio, dan Project.
  • Buka dan buat banyak dokumen di tab baru di jendela yang sama, bukan di jendela baru.
  • Meningkatkan produktivitas Anda sebesar 50%, dan mengurangi ratusan klik mouse untuk Anda setiap hari!

 

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