VBA – Compare Dates
Check if Date is Inbetween Dates
The following VBA function will check if a given date is between two other dates and if so, it will return the value TRUE. If the date is not in between the dates, then it will return the value FALSE.
Function Compare_Dates(Start_Date As Date, End_Date As Date, Other_Date As Date) As Boolean
' Boolean Function to compare dates
'Will return TRUE only when Other_Date is between Start_Date and End_Date
'Otherwise will return FALSE
'Set outcome to FALSE - default value
Compare_Dates = False
If ((Other_Date >= Start_Date) And (Other_Date <= End_Date)) Then
'If Other Date is between Start and End Date then set to true
Compare_Dates = True
The function has three arguments:
• Start_ date – the earliest allowable date
• End_ Date – the latest allowable date
• Other_Date – the date being compared
The function must be in an Excel module. It can be run from an Excel Workbook e.g:
So Compare_Dates(25-04-2007,12-07-2008, 23-06-2006) will give the value TRUE as 23-06-2006 is between 25-04-2007 and 12-07-2008.
Likewise Compare_Dates(19-07-2003,12-12-2001,12-08-2008) will return FALSE as 12-12-2001 is NOT between 19-07-2003 and 12-08-2008
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Did you find this VBA tutorial useful? Then share it with your friends and colleagues: