Error Trying To Use DateDiff

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,202
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
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
14,761
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
4,202
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
14,761
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
4,202
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Ahhh. I had my arguments reversed and corrected, is working. Thank you again!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,501
Messages
5,832,079
Members
430,110
Latest member
Chyke_mxl

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
Top