Error Trying To Use DateDiff

Ark68

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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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
 
Upvote 0
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.
 
Upvote 0
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()
 
Upvote 0
Ahhh. I had my arguments reversed and corrected, is working. Thank you again!
 
Upvote 0

Forum statistics

Threads
1,213,529
Messages
6,114,155
Members
448,554
Latest member
Gleisner2

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