Prevent anything but a Sunday date from being selected

bh24524

Active Member
Joined
Dec 11, 2008
Messages
365
Office Version
  1. 365
  2. 2007
Hi so I am wondering if there is any feature or other that I can utilize in an excel sheet that will make it so that employees when filling out a particular field in the excel form, have to put a Sunday date in a particular cell. I want it to prevent them from putting any other date in that week - it must absolutely be a Sunday date in current year. Is there something that can be used for this?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You can use Data Validation for that.
In DV select "Custom" & use this formula
=AND(YEAR(G3)=2019,WEEKDAY(G3,1)=1)

Change G3 to the cell with the DV
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. I assumed the cell with the date is cell A1. Change this cell in the code (in red) to suit your needs. Close the code window to return to your sheet.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("[COLOR="#FF0000"]A1[/COLOR]")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If Weekday(Target) <> 1 Then
        MsgBox ("You must enter a 'Sunday' date.")
        Target.ClearContents
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. I assumed the cell with the date is cell A1. Change this cell in the code (in red) to suit your needs. Close the code window to return to your sheet.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("[COLOR=#FF0000]A1[/COLOR]")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If Weekday(Target) <> 1 Then
        MsgBox ("You must enter a 'Sunday' date.")
        Target.ClearContents
    End If
    Application.EnableEvents = True
End Sub

Thank you so much, that will work PERFECTLY!
 
Upvote 0
You can use Data Validation for that.
In DV select "Custom" & use this formula
=AND(YEAR(G3)=2019,WEEKDAY(G3,1)=1)

Change G3 to the cell with the DV
Thanks for this solution, for some reason i did not see it when i clicked the link in my email, but I can definitely present this as an option as well.
 
Upvote 0
You're welcome & thanks for the feedback.

When you click the link in the email, it will take you to the last post in the thread, so it's always a good idea to scroll up & check you haven't missed anything. :)
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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