Date data validation formula

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,559
Office Version
365
Platform
Windows
I added another feature in the code. If you pick a wrong date then it will pop up a message to pick another date & also show other relevant dates (which would be violated when you pick that specific date).

Code:
[FONT=lucida console][COLOR=Royalblue]Private[/COLOR] [COLOR=Royalblue]Sub[/COLOR] Worksheet_Change([COLOR=Royalblue]ByVal[/COLOR] Target [COLOR=Royalblue]As[/COLOR] Range)
[COLOR=Royalblue]If[/COLOR] Target.Cells.CountLarge <> [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]Then[/COLOR] [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Sub[/COLOR]

    [COLOR=Royalblue]If[/COLOR] [COLOR=Royalblue]Not[/COLOR] Intersect(Target, Range([COLOR=brown]"A1:A10"[/COLOR])) [COLOR=Royalblue]Is[/COLOR] [COLOR=Royalblue]Nothing[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        [COLOR=Royalblue]Dim[/COLOR] c [COLOR=Royalblue]As[/COLOR] Range, flag [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Boolean[/COLOR], tx [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]String[/COLOR]
        [COLOR=Royalblue]If[/COLOR] IsDate(Target) [COLOR=Royalblue]Then[/COLOR]
            flag = [COLOR=Royalblue]True[/COLOR]: tx = [COLOR=brown]""[/COLOR]
            [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] c [COLOR=Royalblue]In[/COLOR] Range([COLOR=brown]"A1:A10"[/COLOR])
                 
                [COLOR=Royalblue]If[/COLOR] Len(c) > [COLOR=crimson]0[/COLOR] [COLOR=Royalblue]And[/COLOR] IsDate(c) [COLOR=Royalblue]And[/COLOR] c.Address <> Target.Address _
                [COLOR=Royalblue]And[/COLOR] (c.Offset(, [COLOR=crimson]1[/COLOR]) = [COLOR=brown]"Whole venue"[/COLOR] [COLOR=Royalblue]Or[/COLOR] Target.Offset(, [COLOR=crimson]1[/COLOR]) = [COLOR=brown]"Whole venue"[/COLOR] [COLOR=Royalblue]Or[/COLOR] _
                c.Offset(, [COLOR=crimson]1[/COLOR]) = Target.Offset(, [COLOR=crimson]1[/COLOR])) [COLOR=Royalblue]Then[/COLOR]
                    [COLOR=Royalblue]If[/COLOR] Abs(DateDiff([COLOR=brown]"d"[/COLOR], [COLOR=Royalblue]CDate[/COLOR](Target), [COLOR=Royalblue]CDate[/COLOR](c))) < [COLOR=crimson]7[/COLOR] [COLOR=Royalblue]Then[/COLOR]
                    tx = tx & c & [COLOR=brown]" : "[/COLOR] & c.Offset(, [COLOR=crimson]1[/COLOR]) & vbLf
                    flag = [COLOR=Royalblue]False[/COLOR]
                    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
                [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
            [COLOR=Royalblue]Next[/COLOR]
            
            [COLOR=Royalblue]If[/COLOR] flag = [COLOR=Royalblue]False[/COLOR] [COLOR=Royalblue]Then[/COLOR]
                    MsgBox [COLOR=brown]"Pick another date."[/COLOR] & vbLf & [COLOR=brown]"This has been allocated:"[/COLOR] & vbLf & tx, vbCritical
                    Application.EnableEvents = [COLOR=Royalblue]False[/COLOR]
                    Target.Activate
                    Target.ClearContents
                    Application.EnableEvents = [COLOR=Royalblue]True[/COLOR]
                    [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
            [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
        
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
[/FONT]
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Ceallach

New Member
Joined
Sep 14, 2019
Messages
20
Hi! Sorry I didn't see this update sooner, that's a really cool change. :) I'll give it a try!
 

Ceallach

New Member
Joined
Sep 14, 2019
Messages
20
I have encountered a problem!

If I have a "Green room" event booked for 14/11/2019 it still lets me book a "full venue" event for 15/11/2019. Even though the Green room is already booked. It works the other way around e.g. if the whole venue event is booked first it won't let the other one be booked. But basically the first event should take precedence.
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,559
Office Version
365
Platform
Windows
If I have a "Green room" event booked for 14/11/2019 it still lets me book a "full venue" event for 15/11/2019.
Make sure that all the text/terms are the same, it's case sensitive, and check maybe there are extra/trailing space .
In the example you gave me the term is "Whole venue" not "full venue".
 

Ceallach

New Member
Joined
Sep 14, 2019
Messages
20
Sorry, you're right, it's definitely whole venue not full venue! So if a part venue is booked it shouldn't let the whole venue be booked at all with the 7 days, right? Because there's a few other part venues too, not just the examples I gave. Do the specific ones need to be listed somewhere, or is it set so *any* of the part ones are not compatible with "Whole venue' being booked? (Which is correct).
 

Ceallach

New Member
Joined
Sep 14, 2019
Messages
20
Another question, is there a way (in a separate sheet) if I had the Months of the year laid out (sort of calendar style) to have it capture the dates entered for that particular month, but in date order? So at a glance if a manager or somebody wanted to see which dates were booked for "November" they can just look at that and see which weeks have bookings? Ideally I don't want them to need to run a macro or play with a pivot table, just have something that auto-populates from the data.
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,559
Office Version
365
Platform
Windows
Sorry, you're right, it's definitely whole venue not full venue!
Nope, it's "Whole venue", remember it's case sentitive.

Do the specific ones need to be listed somewhere
No need.
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,559
Office Version
365
Platform
Windows
I think you may want to put data validation in col B (listing all the options) to make sure that the user use the exact same terms.
 

Ceallach

New Member
Joined
Sep 14, 2019
Messages
20
I think you may want to put data validation in col B (listing all the options) to make sure that the user use the exact same terms.
Ok I'm still having the problem


If I enter: 1/10/19 Green Room


it then allows me to enter


1/10/19 on the next row as well with no warning, and I can put Whole venue.


But if the first one is Whole venue it won't let me enter the same date again.



All I've changed in the code is instead of A1:A10 my range is B1:B99 because I have a new column A (for a different purpose) so it's all moved along one. And because we will need more entries than 10 obviously. But that shouldn't affect anything else? Not sure what I've done!
 

Forum statistics

Threads
1,089,599
Messages
5,409,207
Members
403,256
Latest member
Viq

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top