0% found this document useful (0 votes)
53 views4 pages

Compare Dates and Times in Excel VBA - Easy Excel Macros

Compare Dates and Times in Excel VBA - Easy Excel Macros

Uploaded by

Pintu Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views4 pages

Compare Dates and Times in Excel VBA - Easy Excel Macros

Compare Dates and Times in Excel VBA - Easy Excel Macros

Uploaded by

Pintu Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

3/1/2015

CompareDatesandTimesinExcelVBAEasyExcelMacros

Excel
Easy
#1Exceltutorialonthenet
Excel
Introduction
Basics
Functions
DataAnalysis
VBA
300Examples
Askus

CompareDatesandTimes

Follow

3.9k

ThisexampleteachesyouhowtocomparedatesandtimesinExcelVBA.Datesandtimesare
storedasnumbersinExcelandcountthenumberofdayssinceJanuary0,1900.Whatyousee
dependsonthenumberformat.
1.EntersomenumbersincolumnA.

2.Thesenumbersaredates.ThisisaperfectwaytoentersomedateswithoutworryingabouttheDate
format.ChangetheformattoDate(RightclickonthecolumnAheader,FormatCellsandchoose
Date).
Result:

http://www.exceleasy.com/vba/examples/comparedatestimes.html

1/5

3/1/2015

CompareDatesandTimesinExcelVBAEasyExcelMacros

Note:DatesareinUSFormat.Monthsfirst,DaysSecond.Thistypeofformatdependsonyour
windowsregionalsettings.
Placeacommandbuttononyourworksheetandaddthefollowingcodelines:
3.DeclarethevariableioftypeInteger.
DimiAsInteger
4.AddaForNextloop.
Fori=1To5
Nexti
5.TheDatefunctionreturnsthecurrentdatewithoutthetime.Addthefollowingcodelinetothe
loop,tohighlightallthecellscontainingthecurrentdate(12/22/2013).
IfCells(i,1).Value=DateThenCells(i,1).Font.Color=vbRed
Result:

6.Addthefollowingcodelinetotheloop,tohighlightallthedatesearlierthan04/19/2011.
IfCells(i,1).Value<DateValue("April19,2011")ThenCells(i,1).Font.Color=vbRed
Result:

http://www.exceleasy.com/vba/examples/comparedatestimes.html

2/5

3/1/2015

CompareDatesandTimesinExcelVBAEasyExcelMacros

7.Butwhatabouttimes,wehearyousay.Theyarethedecimals.SwitchbacktoGeneralformatand
changethenumberstodecimalnumbers.

8.Nowchangetheformatto'DateandTime'format.

Result:
http://www.exceleasy.com/vba/examples/comparedatestimes.html

3/5

3/1/2015

CompareDatesandTimesinExcelVBAEasyExcelMacros

9.Ifyouwanttohighlightallcellscontainingthecurrentdate,wecannotusethecodelineat5
anymore.Whynot?BecausethenumbersincolumnAaredecimalnumbersnow.Comparingitwith
Date(awholenumber)wouldnotgiveanymatch.(Itwouldonlygiveamatchwith12/22/2013at
midnightexactly!)Thefollowingcodelinedoeswork:
IfInt(Cells(i,1).Value)=DateThenCells(i,1).Font.Color=vbRed
Explanation:wesimplyusetheIntfunction.TheIntfunctionroundsanumberdowntothenearest
integer.ThiswaywecangetthedateswithoutthetimesandcomparethesedateswithDate.
Result:

10.Addthefollowingcodelinetohighlightallthecellscontainingtimesinthemorning.
If(Cells(i,1).ValueInt(Cells(i,1).Value))<0.5ThenCells(i,1).Font.Color=vbRed
Explanation:weonlyneedthedecimalssothereforewesubtracttheintegerpart.Noon(halfway
throughtheday)isrepresentedas0.5.Decimalslowerthan0.5arethetimesinthemorning.
Result:

Doyoulikethisfreewebsite?PleasefollowusonGoogle+
Follow

3.9k

http://www.exceleasy.com/vba/examples/comparedatestimes.html

4/5

You might also like