+ Reply to Thread
Results 1 to 10 of 10

Have Autorun Macros but can we stop them running at one particular interval

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Have Autorun Macros but can we stop them running at one particular interval

    Hello Everyone,

    I have two AUTORUN macro's ... one runs every 15 secs and one run every 15 mins. These two are running (auto running) without any conflict and I'm happy but I have a specific need --------- I want these two NOT to run at one particular interval (every Friday from 8:55 AM to 10:15 AM). I have managers meeting during this time and I don't want this sheet get updated (as this will be projected on a big monitor).

    Below goes the two autorun macro's ----- can someone please modify such that these two not during that one interval ---- I would really appreciate if someone can dig it. Thanks a lot in advance!!

    Sub RunEvery15Mins()
    
        Call Workbook_Open
    
        Application.OnTime Now + TimeValue("00:4:00"), "RunEvery15Mins"
    
    End Sub

    Dim VA, VT, Rg As Range
    
    Private Sub Display()
        If Rg Is Nothing Or IsEmpty(VA) Then Exit Sub
        ActiveWindow.ScrollRow = Rg.Row
        Set Rg = Rg.End(xlDown):  If IsEmpty(Rg) Then Set Rg = [B3]
        VT = Now + VA
        Application.OnTime VT, "ThisWorkbook.Display"
    End Sub
    
    Sub ScrollStart()
        Set Rg = [B3]
        VA = 15 / 86400
        ActiveWindow.ScrollColumn = 1
        Display
    End Sub
    
    Sub ScrollStop()
        Application.OnTime VT, "ThisWorkbook.Display", , False
        Set Rg = Nothing
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        If Not Rg Is Nothing Then ScrollStop
    End Sub

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Have Autorun Macros but can we stop them running at one particular interval

    If the fact that the application ontime is still active, you could add an IF statement that checks if the VT value is a Friday and the VT time falls between 8:55 AM and 10:15 AM
    You'll have to play around with that, it's just an idea that popped up
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Have Autorun Macros but can we stop them running at one particular interval

    Private Sub Display()
        If Rg Is Nothing Or IsEmpty(VA) Then Exit Sub
        ActiveWindow.ScrollRow = Rg.Row
        Set Rg = Rg.End(xlDown):  If IsEmpty(Rg) Then Set Rg = [B3]
        If Weekday(Date) = vbFriday And Time >= TimeValue("8:55 AM") And Time < TimeValue("10:15 AM") Then
            VT = Date + TimeValue("10:15 AM")
        Else
            VT = Now + VA
        End If
        Application.OnTime VT, "ThisWorkbook.Display"
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Re: Have Autorun Macros but can we stop them running at one particular interval

    Thank you very much!!! this helps me a lot!!

    Making this to resolved!!

  5. #5
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Re: Have Autorun Macros but can we stop them running at one particular interval

    Hi There,

    I'm reopening this thread not because of my original questions which I posted earlier (those 2 things have been resolved) but I have new ask and this one is on the similar lines as my earlier. Also, this could be a simple one and that's the reason I don't want to open up a new thread. Hope I'm not violating any procedures here

    Ask:
    I wanted a macro to autorun on a particular day and at particular time ---- Every Friday at 8:47 AM. Below is the codes (1) and (2) I have written based on the similar on what was given to me earlier. Not sure what is wrong but this is not working. Can you please look into it and correct this. Thanks in advance!!

    Code-1
    Sub RunOneTimeOnFridayMorn()
    
        Call Workbook_Open
    
            If Weekday(Date) = vbFriday And Time = TimeValue("08:47 AM") Then
                LapTime2 = Date + TimeValue("08:47 AM")
            End If
    
                Application.OnTime LapTime2, "RunOneTimeOnFridayMorn"
    End Sub
    Code 2
    Sub RunOneTimeOnFridayMorn()
    
        Call Workbook_Open
    
            If Weekday(Date) = vbFriday And Time = TimeValue("08:47 AM") Then
                LapTime2 = Date + TimeValue("08:47 AM")
                Application.OnTime LapTime2, "RunOneTimeOnFridayMorn"
            End If
    End Sub
    Last edited by NametobeRenamed; 11-01-2019 at 04:36 PM.

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Have Autorun Macros but can we stop them running at one particular interval

    If you want it to run on a friday at a specific time, just set that in a variable and set the application ontime to that date and time and forget about checking if then else.

    The If then else was good for your first question because you wanted it NOT to run between two values.

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Have Autorun Macros but can we stop them running at one particular interval

    What's in Workbook_Open ???
    What's the name of the macro you want to run on friday morning?

    My idea:

    Place the code below in a Module
    The Red line of code is the topmost outside the subs

    Global LapTime2     As Date
    
    Public Sub setFridayMorningRun()
    Dim tDays   As Integer
    tDays = Choose(Weekday(Date), 5, 4, 3, 2, 1, 0, 6)
    If tDays = 0 Then
        If Time > TimeValue("08:47 AM") Then tDays = tDays + 7
    End If
    LapTime2 = (Date + tDays) + TimeSerial(8, 47, 0)
    Application.OnTime LapTime2, "RunOneTimeOnFridayMorn"
    End Sub
    
    Public Sub stopFridayMorningRun()
    On Error Resume Next
    Application.OnTime Earliesttime:=LapTime2, Process:="RunOneTimeOnFridayMorn", Schedule:=Flase
    Err.Clear
    On Error GoTo 0
    End Sub
    
    Public Sub RunOneTimeOnFridayMorn()
    ' here the code you want to run on friday morning
    
    
    
    ' when it completes you run setFridayMorningRun again
    setFridayMorningRun
    End Sub
    In the Workbook_Open event

    Private Sub Workbook_Open()
    setFridayMorningRun
    End Sub
    So every time the workbook is opened the macro is set to run on a friday at 8 47 AM

    I also suggest you place this in the

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    stopFridayMorningRun
    End Sub

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Have Autorun Macros but can we stop them running at one particular interval

    Another way to calc the next Friday

    Public Sub setFridayMorningRun()
        LapTime2 = Date + 8 - Weekday(Date, vbFriday) + TimeValue("08:47 AM")
        If LapTime2 - Now > 7 Then LapTime2 = LapTime2 - 7
        Application.OnTime LapTime2, "RunOneTimeOnFridayMorn"
    End Sub

  9. #9
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Have Autorun Macros but can we stop them running at one particular interval

    @AlphaFrog: true,much simpler,

  10. #10
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Re: Have Autorun Macros but can we stop them running at one particular interval

    Thank you very much Keeballah and AlphaFrog!!

    You both helped me a lot ... once again thank you!!

+ 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. [SOLVED] Event Driven Macros stop running after running a routine macro
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2017, 12:34 PM
  2. Autorun macros
    By Graebeard in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-19-2010, 12:03 AM
  3. Web queries, Macros, Stop auto-running, Userforms
    By Flashart in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2008, 03:51 PM
  4. Autorun macros on opening
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2006, 02:11 PM
  5. How do I prevent AutoRun Macros when Programmatically Opening Workbook?
    By Joseph Geretz in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 03-26-2006, 06:30 PM
  6. Stop Macros from Running When Workbook Opened via Automation
    By Google Boy of Company C in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-15-2005, 12:05 PM
  7. [SOLVED] AutoRun Excel Macros
    By JD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-26-2005, 05:19 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