How I run my code at specific time without interfere from me

leap out

Active Member
Joined
Dec 4, 2020
Messages
287
Office Version
  1. 2016
  2. 2010
Hi

I would like to run my code automatically at time PM17:00 . when I try to click button 1 so should show message how many remaining time to running the code . and when reach to the PM17:00 then should run the code automatially without click button 1 .

thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Final version
VBA Code:
Option Explicit

Dim RunAt As Variant
Dim InProgress As Boolean

Sub InitializeMacro()
    Dim DefTime As Date

    DefTime = TimeSerial(15, 8, 0)

    If IsEmpty(RunAt) Then
        If Time > DefTime Then
            RunAt = Date + 1 + DefTime
        Else
            RunAt = DefTime
        End If

        InProgress = True
        Application.OnTime RunAt, "MyMacro"

        MsgBox "Countdown has started", vbInformation

    ElseIf InProgress Then
        MsgBox "There is " & Format(RunAt - Time, "hh:mm:ss") & " left to run MyMacro.", vbInformation
    End If

End Sub


Sub Auto_Open()
    Call InitializeMacro
End Sub


Sub Auto_Close()

    On Error Resume Next
    'reset of initiated task
    Application.OnTime RunAt, "MyMacro", , False
    'If the workbook was closed after MyMacro was executed or
    'the countdown procedure has not been initialized,
    'then the error handler will ignore the task reset


    'cleaning these variables is not necessary,
    'cause the file is closed, so the variables will reset themselves.
    RunAt = Empty
    InProgress = False
End Sub


Sub MyMacro()
    InProgress = False
    RunAt = Empty

    MsgBox "Hello"

    'if you want to automatically define a new task for the next day
    'then uncomment the bottom line:
    Application.OnTime Now, "InitializeMacro"
End Sub

The InitializeMacro macro should be attached to the button.

Artik
 
Upvote 0
Solution

Forum statistics

Threads
1,221,481
Messages
6,160,083
Members
451,616
Latest member
swgrinder

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