+ Reply to Thread
Results 1 to 5 of 5

VBA code required not to execute during certain date and time range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-20-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    275

    Arrow VBA code required not to execute during certain date and time range

    Dear all,
    I want to restrict my VBA module to not to run between certain date and time range of every month, irrespective of which year it is.
    Like, a pop-up message must tell the user that this module cannot be used now.
    Date restriction is: Between 15th to 20th of every month
    (irrespective of month and year)
    Time restriction is: Between 10AM to 12PM everyday.

    Include these conditions in nested IF loops.

    Plz help.
    Thanks.

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA code required not to execute during certain date and time range

    Option Explicit
    
    Sub dateIf()
        Dim DateToday As Date
        
        DateToday = Date
    
        If Day(Date) >= 15 Or Day(Date) <= 20 Then
            MsgBox Date & " is between the 15th and 20th of the month so the vba will exit"
                Exit Sub
        End If
        
        'Do other Work if NOT between that date
        
    End Sub
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: VBA code required not to execute during certain date and time range

    Maybe this.....
    Sub YourMacroName()
    
    'Variable declaration required for your code
    
    
    If TimeValue(Now) < TimeValue("10:00:00") And TimeValue(Now) > TimeValue("00:00:00") Then
        If Day(Date) < 15 And Day(Date) > 20 Then
    
            'your code here
    
    
            MsgBox "You can run the code." 'Example code
        Else
            MsgBox "You can only run this code before 15th and after 20th of any month.", vbCritical, "Code Execution Not Allowed!"
        End If
    Else
        MsgBox "You can only run this code between 12:00:01 AM To 09:59:59 AM.", vbCritical, "Code Execution Not Allowed!"
    End If
    End Sub
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  4. #4
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA code required not to execute during certain date and time range

    Ahh, I must have missed the time restriction

  5. #5
    Forum Contributor
    Join Date
    09-20-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    275

    Re: VBA code required not to execute during certain date and time range

    Thanks....Thats works like a charm....Thanks for all your support

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. efficiency recomendations, code taking long time to execute...
    By am_hawk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-04-2013, 03:55 PM
  2. Replies: 0
    Last Post: 07-17-2012, 10:11 AM
  3. [SOLVED] Run-time error '28' Out of stack space - Code alteration help required!
    By PaulThomson in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-18-2012, 03:20 PM
  4. Macro code for date & time range copy
    By nmnijam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-15-2009, 04:42 PM
  5. Run a stopwatch for time then execute code
    By JFamilo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-03-2005, 10:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1