Error Trying To Use DateDiff

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,751
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am getting an "Invalid procedure call or argument" error with line of code. I am hoping someone is able and willing to help me correct it.

VBA Code:
cdt = Now
If DateDiff(h, cdt, dc_rmr1) > 24 Then     '<--- ERROR!
      l = InStr(.Cells(trow, 9), ":")
      llen = Len(.Cells(trow, 9))
      .Cells(trow, 9).Characters(l, llen).Font.Color = c_red
End If

I'm trying to determine if the difference between two dates (cdt and dc_rmr1) is greater than 24 hours.

cdt = 2020-07-12 10:01:04 AM
dc_rmr1 = 44022.6614583333
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
11,619
Office Version
  1. 365
Platform
  1. Windows
h should be in double quotes.

I have seen some unpredictable accuracy problems with rounding when using datediff, personally I would use this in an effort to try and eliminate such errors.
VBA Code:
If DateDiff("s", cdt, dc_rmr1) > 86400 Then
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,751
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hey Jasonb75, thank you so much. Your suggestion managed to eliminate the error, which was what prompted me plea for help.
But now without the error, I'm not getting the results I am expecting.

cdt = July 12, 2020 10:01:04 AM
dc_rmr1 = July 10, 2020 15:52:30 PM

Clearly there is more than 24 hours diffeence between the two dates, but my code is processing it as false and my code associated to a positive isn't being executed.
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
11,619
Office Version
  1. 365
Platform
  1. Windows
If you only want TRUE when dc_rmr1 is more than 24 hours in the past then you need to swap it with cdt.
If you want TRUE for differences of 24 hours past and future then wrap the DateDiff formula in ABS()
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,751
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Ahhh. I had my arguments reversed and corrected, is working. Thank you again!
 

Watch MrExcel Video

Forum statistics

Threads
1,118,957
Messages
5,575,230
Members
412,651
Latest member
caitlincole
Top