Skip i'r prif gynnwys

Sut i ganiatáu fformat dyddiad yn unig mewn celloedd penodol?

Sut allech chi gyfyngu rhestr o gelloedd i ganiatáu fformat dyddiad yn unig yn nhaflen waith Excel? Yn yr erthygl hon, byddaf yn siarad am rai triciau diddorol ar gyfer delio â'r swydd hon.

Caniatáu fformat dyddiad yn unig mewn celloedd penodol sydd â swyddogaeth Dilysu Data

Caniatáu fformat dyddiad yn unig mewn celloedd penodol sydd â chod VBA


swigen dde glas saeth Caniatáu fformat dyddiad yn unig mewn celloedd penodol sydd â swyddogaeth Dilysu Data

Yn Excel, mae'r Dilysu Data yn swyddogaeth bwerus a all eich helpu i greu rhestr ostwng, i atal cofnodion dyblyg, ac ati. Gall hefyd eich helpu i atal mynd i mewn i fformat data arall ond dim ond fformat dyddiad mewn celloedd penodol. Gwnewch y camau canlynol:

1. Cliciwch Dyddiad > Dilysu Data > Dilysu Data, gweler y screenshot:

doc caniatáu dyddiad 1

2. Yn y Dilysu Data blwch deialog, cliciwch Gosodiadau tab, ac yna dewis Custom oddi wrth y Caniatáu rhestr ostwng, yna nodwch y fformiwla hon: = AC (ISNUMBER (B2), CHWITH (CELL ("fformat", B2), 1) = "D") i mewn i'r Fformiwla blwch testun, gweler y screenshot:

doc caniatáu dyddiad 2

Nodyn: Yn y fformiwla uchod, B2 yw cell gyntaf y golofn yr ydych am gyfyngu ar y fformat data.

doc caniatáu dyddiad 3

3. Yna ewch ymlaen i glicio Rhybudd Gwall tab yn y blwch deialog, gwnewch y gweithrediadau canlynol:

(1.) Gwiriwch Dangos rhybudd gwall ar ôl mewnbynnu data annilys opsiwn;

(2.) O dan y arddull gwympo, dewiswch Stop opsiwn;

(3.) Teipiwch y Teitl ac Neges gwall eich bod am arddangos yn y blwch prydlon.

4. Ar ôl gorffen y gosodiadau, cliciwch OK botwm, ac yn awr, pan fyddwch yn mewnbynnu'r data nad yw'n fformat dyddiad go iawn, bydd blwch prydlon yn galw allan i'ch atgoffa fel y llun a ganlyn a ddangosir:

doc caniatáu dyddiad 4


swigen dde glas saeth Caniatáu fformat dyddiad yn unig mewn celloedd penodol sydd â chod VBA

Yr ail ddull, byddaf yn cyflwyno cod VBA i chi.

1. Gweithredwch y daflen waith rydych chi am ei defnyddio.

2. Yna cliciwch ar y dde ar y tab dalen, a dewis Gweld y Cod o'r ddewislen cyd-destun, gweler y screenshot:

doc caniatáu dyddiad 5

3. Yn yr arddangos Microsoft Visual Basic ar gyfer Ceisiadau ffenestr, copïo a gludo'r cod isod i'r Modiwlau, gweler y screenshot:

Cod VBA: Dim ond caniatáu fformat dyddiad sy'n mynd i mewn i gelloedd penodol:

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 caniatáu dyddiad 6

4. Yna arbedwch hysbyseb cau'r cod, nawr, pan fyddwch chi'n nodi'r gwerth nad yw'n fformat dyddiad yn ystod B2: B12, bydd blwch prydlon yn popio allan i'ch atgoffa, a bydd gwerth y gell yn cael ei glirio ar yr un pryd, gweler y screenshot:

doc caniatáu dyddiad 7

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!

 

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