VBA check to see if time is between 2 values

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am having trouble in VBA. First of all I stink with times! I need to check to see if time falls between 2 values, 7:30 AM and 3:30 PM. Can someone please guide me on this one! Thanks in advance!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
times are always less then 1. If its over one then those are days, in essence 0.31 to 0.65, you can get more detailed than that
 
Upvote 0
Working with time, I usually use the MOD function within the worksheet to separate the date from the time. In VBA, you can create a custom function to replicate said function.

Code:
Sub TestTime()
Dim dDate As Date
Dim dTime As Date


dDate = Now
dTime = XLMod(dDate, 1)
Select Case dTime
    Case TimeValue("7:30:00") To TimeValue("15:30:00")
        MsgBox True
    Case Else
        MsgBox False
End Select


End Sub


Function XLMod(a, b)
    ' This replicates the Excel MOD function
    XLMod = a - b * Int(a / b)
End Function
 
Last edited:
Upvote 0
Hello Stephen,

VBA has several functions for dealing with Dates and Times. As mentioned, the time portion of a date is always a fraction expressed in seconds since midnight.

Here is a sample macro using TimeValue to extract the time portion of a Date or a time string. You can then directly compare the times.

Code:
Sub TimeTest()


    Dim D1 As Date
    Dim T1 As Date
    Dim T2 As Date
    Dim x  As Variant
    
        D1 = TimeValue("6/19/2017 6:00AM")
        T1 = TimeValue("7:30 AM")
        T2 = TimeValue("3:30 PM")
        
        x = (D1 >= T1 And D1 <= T2)
         
End Sub
 
Upvote 0
Thanks to all. Sorry it took me a few days to get back but based on your posts I was able to figure it out. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,226
Members
448,878
Latest member
Da9l87

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