Consulting

Results 1 to 3 of 3

Thread: Solved: Performing a task in vba if time is between 2 times?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location

    Solved: Performing a task in vba if time is between 2 times?

    Hi all i have been trying to execute a piece of code if the workboo is opened after midnight but before 6:50am but i can't seem to manage it, any ideas what i'm doing wrong?[VBA]Dim tm As Date
    tm = Now
    td = Sheets("Shift 1").Cells.Find(What:=Date, After:=Sheets("Shift 1").Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Address
    'check for night shift
    If tm > TimeSerial(0, 0, 1) Or tm < TimeSerial(6, 50, 0) Then
    td = Sheets("Shift 1").Cells.Find(What:=Date, After:=Sheets("Shift 1").Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Offset(-1, 0).Address
    drng = Range("IV" & Range(td).Row).End(xlToLeft).Address
    End If[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I even tried this line[vba]If tm > #12:01:00 AM# And tm < #6:50:00 AM# Then[/vba]but no joy!
    And this line[VBA]If tm > TimeValue("00:01:00") And tm < TimeValue("06:50:00") Then[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Solved it i used[VBA]
    If Time > TimeValue("00:01:00") And Time < TimeValue("06:50:00") Then
    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

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