Restrict date entered

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The max date in Excel is 31 Dec 9999, which corresponds to the value 2958465.

How can I restrict users from entering a date after this value, using just VBA (as opposed to the Data Validation found under Data in the ribbon)?

The code below shows an Overflow error (when the cell I am inputting the date is formatted to Date).

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Value > 2958465 Then

    End If

End Sub

This is the best I can do:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Target.NumberFormat = "General"
    
    If Target.Value > 2958465 Then
    
        Target.Value = vbNullString
        
        MsgBox "too large"
        
    Else
    
        Target.NumberFormat = "dd/mm/yyyy;@"
    
    End If
    
End Sub

Thanks
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I'd assume you'll want to restrict it to a specific area in reality, but basically something like this:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim cell As Range
   For Each cell In Target.Cells
      If cell.Value2 > 2958465 Then
        With Application
           .EnableEvents = False
           .Undo
           .EnableEvents = True
        End With
      End If
   Next

End Sub
 
Upvote 0
Solution
I'd assume you'll want to restrict it to a specific area in reality, but basically something like this:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim cell As Range
   For Each cell In Target.Cells
      If cell.Value2 > 2958465 Then
        With Application
           .EnableEvents = False
           .Undo
           .EnableEvents = True
        End With
      End If
   Next

End Sub
Thanks.

The syntax that makes it work is Value2, as opposed to Value.

So I changed my original code to:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Value2 > 2958465 Then
        
        Application.EnableEvents = False
        
        Application.Undo
        
        Application.EnableEvents = True
        
    End If

End Sub

and it worked.
 
Upvote 0
But will error if you change more than one cell at a time...
 
Upvote 0
But will error if you change more than one cell at a time...
I've restricted the user to only allow one cell at a time with:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Target(1, 1).Select
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,183
Messages
6,123,529
Members
449,105
Latest member
syed902

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top