Macro To Stop me from using dates that are already in use?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

Not sure if this is possible, but hoping for best ideas.

I have a sheet I input Start dates and end dates into for my projects
Because one of my superpowers is not Duplicity I can only be in one place at a time but sometimes my brain forgets that!

So Simplisest solution when i choose to leave the page, check if any of the dates overlap?

So Startdates are down Column F from row 7 and End dates are Column G

if it helps the area i need checking is only F7:G16

any ideas

thanks

Tony
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
If you go to Data menu, and click Data Tools -> Data Validation, there is an option to set a rule where the validation type is Date, and you can set from and to dates, which can feed from other cells.

If you want to check all of your dates, you can use Custom validation type, and then enter your rule as a formula that checks max/min dates. This way, you can set it up to prevent you from entering a date in a range, but it might be a pretty complex formula.

Or, you can also set up some kind of conditional formatting to alert you that you entered an overlap / conflicting appointment.

Which way do you want to take?
 
Upvote 0
.
If I understand your requirements correctly .... place this macro into the affected worksheet module. You will need to change
the sheet name in the macro as required :

Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)


    Dim rCriteria As Range
    Dim rData As Range
    Dim c As Range, R As Range
    Dim sFirstAddress As String
    Dim ColorCounter As Long
    Dim StartTime As Single, EndTime As Single


Set rCriteria = Sheets(1).Range("F7:F17")
Set rData = Sheets(1).Range("G7:G17")


Application.ScreenUpdating = False


With rData
    .Interior.ColorIndex = xlNone


For Each R In rCriteria
    If Not R = "" Then
    Set c = .Find(what:=R.Value, LookIn:=xlValues, lookat:=xlWhole, _
            searchdirection:=xlNext)
    If Not c Is Nothing Then
        sFirstAddress = c.Address


        Do
            Set c = .FindNext(c)
            c.Interior.Color = vbYellow
            MsgBox "Duplicate Value ! ", vbCritical, "Duplicate Value"
            ColorCounter = ColorCounter + 1
        Loop Until c.Address = sFirstAddress
    End If
    End If
Next R


End With


Application.ScreenUpdating = True


End Sub
 
Upvote 0
Could you just use Conditional Formatting?
Select F7:G16 and apply the CF shown.

Excel Workbook
FG
6StartEnd
71/01/20175/08/2017
86/08/201731/08/2017
915/09/20171/01/2018
1027/08/20175/09/2017
1122/02/201828/02/2018
1228/02/20185/03/2018
1319/03/20182/04/2018
144/03/20186/06/2018
15
16
Overlap Dates
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F71. / Formula is =(SUMPRODUCT(($F$7:$F$16<$G7)*($G$7:$G$16>$F7))>1)*(COUNT($F7:$G7)=2)Abc
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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