VBA syntax to check between two time frames i.e. 2 PM thru 5 AM?

L

lwchapman

I am using a worksheet code page and would like to check if the current time
of day falls between 2:00:00 PM thru 5:00:00 AM. If true, then a procedure
would be executed. If false, the procedure would not run. I am having
trouble with my syntax and cannot find the correct coding, function or "how
to" in MS Help.

I am using Excel in Office 2007 Professional.

I would greatly appreciate any suggestions one might have to offer in
resolving this issue.

Thanking all in advance, Larry
 
P

Peter T

Sub test()
Dim t1 As Date, t2 As Date
Dim tm As Date

tm = Now

''' also uncomment each of the following starting from
''' the top to test

'tm = TimeSerial(5, 0, 0)
'tm = TimeSerial(5, 0, 1)
'tm = TimeSerial(13, 59, 59)
'tm = TimeSerial(14, 0, 0)

If tm > 1 Then
tm = tm - Int(tm)
End If

If tm >= TimeSerial(14, 0, 0) Or tm <= TimeSerial(5, 0, 0) Then
MsgBox "Do it", , tm
Else
MsgBox "skip it", , tm
End If

End Sub

Regards,
Peter T
 
J

Jim Rech

Maybe:

If Hour(Now) >= 14 Or Hour(Now) < 5 Then

--
Jim
|I am using a worksheet code page and would like to check if the current
time
| of day falls between 2:00:00 PM thru 5:00:00 AM. If true, then a
procedure
| would be executed. If false, the procedure would not run. I am having
| trouble with my syntax and cannot find the correct coding, function or
"how
| to" in MS Help.
|
| I am using Excel in Office 2007 Professional.
|
| I would greatly appreciate any suggestions one might have to offer in
| resolving this issue.
|
| Thanking all in advance, Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top