Subtracting Time

MattOCUSA

Board Regular
Joined
Jan 12, 2007
Messages
231
If I have a worsksheet with:

Range("A1") = 11:30 PM and it is formatted to Time in A1, and I want to know if the NOW time is less than or equal to 5 minutes passed the time in cell A1, how the heck do I do that using VBA?

I'm taking Msgbox readings now from my cell value in A1 and it says: 130.34532452 where I would expect it would say 11:30PM? I cant get it to do any math that makes any sense using NOW with it either? I'd like to do something like:

Code:
 If Range("A1") - Now > 5 Then
    Msgbox "It's beyond 5 minutes"
Else
    Msgbox "It's only been " & Now - Range("A1") & " minutes."
End If

Obviously that code exactly doesn't work but something like that is what I want to do.

Thank You for any help!
Matt
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi

Now brings over the entire date (ie so not just the time) - you need to use Time:

Code:
 Sub test()

If CDbl(Time - Range("A1")) > (5 / (24 * 60)) Then  'convert 5 minutes into decimal number
    MsgBox "It's beyond 5 minutes"
Else
    MsgBox "It's only been " & (Time - Range("A1")) * 24 * 60 & " minutes."
End If

End Sub

Converting to a double also means you get proper negative numbers.
 

Forum statistics

Threads
1,181,719
Messages
5,931,656
Members
436,797
Latest member
fullmetalpogi

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