メインコンテンツへスキップ

特定のセルで日付形式のみを許可するにはどうすればよいですか?

セルのリストを制限して、Excelワークシートに日付形式の入力のみを許可するにはどうすればよいですか? この記事では、この仕事に対処するためのいくつかの興味深いトリックについて説明します。

データ検証機能を使用して、特定のセルで日付形式のみを許可する

VBAコードを含む特定のセルで日付形式のみを許可する


矢印青い右バブル データ検証機能を使用して、特定のセルで日付形式のみを許可する

Excelでは、 データ検証 は、ドロップダウンリストの作成、重複エントリの防止などに役立つ強力な機能です。また、特定のセルに日付形式のみを入力する他のデータ形式の入力を防止するのにも役立ちます。 次の手順で実行してください。

1に設定します。 OK をクリックします。 且つ > データ検証 > データ検証、スクリーンショットを参照してください:

ドキュメント許可日 1

2。 の中に データ検証 ダイアログボックスで、をクリックします。 設定 タブをクリックし、 カスタム 次を許可します。 ドロップダウンリストから、次の式を入力します。 = AND(ISNUMBER(B2)、LEFT(CELL( "format"、B2)、1)= "D") テキストボックス、スクリーンショットを参照:

ドキュメント許可日 2

Note:上記の式では、 B2 データ形式を制限する列の最初のセルです。

ドキュメント許可日 3

3. 次にクリックします エラーアラート ダイアログボックスのタブで、次の操作を実行してください。

(1.)チェック 無効なデータが入力された後にエラーアラートを表示する オプション;

(2.) 形式 ドロップダウン、選択してください Force Stop オプション;

(3.)を入力します 役職 & エラーメッセージ プロンプトボックスに表示したいもの。

4。 設定が終わったら、 OK ボタンをクリックすると、実際の日付形式ではないデータを入力すると、次のスクリーンショットのようにプロンプ​​トボックスが表示されます。

ドキュメント許可日 4


矢印青い右バブル VBAコードを含む特定のセルで日付形式のみを許可する

XNUMX番目の方法では、VBAコードを紹介します。

1。 使用するワークシートをアクティブにします。

2。 次に、シートタブを右クリックして、 コードを表示 コンテキストメニューから、スクリーンショットを参照してください。

ドキュメント許可日 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

ドキュメント許可日 6

4。 次に、コードを閉じて広告を保存します。B2:B12の範囲で日付形式ではない値を入力すると、プロンプトボックスが表示され、同時にセルの値がクリアされます。スクリーンショットを参照してください。

ドキュメント許可日 7

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、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