Results 1 to 3 of 3

Thread: If Now > Time(8, 0, 0) And Now < Time(8, 10, 0) Then

  1. #1

    Thread Starter
    Frenzied Member Robbo's Avatar
    Join Date
    Jan 2001
    Location
    Bradford
    Posts
    1,143

    If Now > Time(8, 0, 0) And Now < Time(8, 10, 0) Then

    Trying to get excel to open and check the time then run some stuff

    If Now > Time(8, 0, 0) And Now < Time(8, 10, 0) Then 'Days
    ThisWorkbook.Worksheets("Home").Unprotect Password:="123"
    ActiveWorkbook.Unprotect "123"
    ThisWorkbook.Worksheets("Home").Range("D37").Locked = True
    End If

    but its running all the time, now do i get to just run in the set times?

    cheers in advance
    -----------------------------------------------
    "The hall is rented,"
    "the orchestra is engaged,"
    "its now time to see if you can dance!"
    Q, Q-Who, Star Trek The Next Generation
    -----------------------------------------------
    General Work day

    -----------------------------------------------
    DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: If Now > Time(8, 0, 0) And Now < Time(8, 10, 0) Then

    The time variables are floating point values between 0 and 1.

    Code:
    ? cdbl(timeserial(8,0,0)), cdbl(timeserial(8,10,0))
     0.333333333333333           0.340277777777778 
    
    ? cdbl(now) ,cdbl(date), cdbl(time)
     44075.4492708333            44075         0.449270833333333
    So it surprises me that the IF statement always returns True
    Because Now will never be less than Time(8, 10, 0)

    Instead of using Now use Time

    I assume you want the code to execute between 08:00:00 and 08:10:00?

  3. #3

    Thread Starter
    Frenzied Member Robbo's Avatar
    Join Date
    Jan 2001
    Location
    Bradford
    Posts
    1,143

    Re: If Now > Time(8, 0, 0) And Now < Time(8, 10, 0) Then

    think worked it out

    If Time() > TimeValue("06:00:00") And Time() < TimeValue("14:00:00") Then 'Days
    ThisWorkbook.Worksheets("Home").Range("D817").Locked = True
    ElseIf Time() > TimeValue("14:00:00") And Time() < TimeValue("22:00:00") Then 'Afternoons
    ThisWorkbook.Worksheets("Home").Range("D37").Locked = True
    ThisWorkbook.Worksheets("Home").Range("D1317").Locked = True
    ElseIf Time() > TimeValue("22:00:00") And Time() < TimeValue("06:00:00") Then 'Nights
    ThisWorkbook.Worksheets("Home").Range("D312").Locked = True
    End If

    this worked



    Quote Originally Posted by Arnoutdv View Post
    The time variables are floating point values between 0 and 1.

    Code:
    ? cdbl(timeserial(8,0,0)), cdbl(timeserial(8,10,0))
     0.333333333333333           0.340277777777778 
    
    ? cdbl(now) ,cdbl(date), cdbl(time)
     44075.4492708333            44075         0.449270833333333
    So it surprises me that the IF statement always returns True
    Because Now will never be less than Time(8, 10, 0)

    Instead of using Now use Time

    I assume you want the code to execute between 08:00:00 and 08:10:00?
    -----------------------------------------------
    "The hall is rented,"
    "the orchestra is engaged,"
    "its now time to see if you can dance!"
    Q, Q-Who, Star Trek The Next Generation
    -----------------------------------------------
    General Work day

    -----------------------------------------------
    DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width