VBA to auto close msgbox or not!?!

Dazlancs

New Member
Joined
Sep 10, 2011
Messages
28
Hi All,

I'm trying to automate a report in Excel 2010 and have got everything working expect for one small problem. Whenever I open the report, it runs the code (which is what I want but bear with me).

I would like to have the workbook automatically open (occurring from Time Scheduler) then give me 10 seconds to press "Yes" to stop the macro and exit the sub or if this doesn't happen, continue the macro.

Any help would be great.

Thanks,
Darren
 

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
598
If it's a standard MsgBox, you will struggle as any code is paused until the user clicks on one of the option buttons.

Maybe design your own MsgBox (i.e. a User Form) and use the Application.OnTime method to instigate a timer which will either cancel the forthcoming operations (if/when the appropriate button is pressed) or proceed with it (if/when another appropriate button is pressed, or the timer runs down)

This thread may help...
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
When your workbook opens, show UserForm1 to display a Yes button (CommandButton1) and message (Label1), and count down timer (Label2)

UserForm code...
Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] UserForm_Initialize()
    [COLOR=green]'Userform Message[/COLOR]
    Me.Label1.Caption = "Click 'Yes' to cancel."
    
    [COLOR=green]'Timer countdown[/COLOR]
    Me.Label2.Caption = 11
    
    [COLOR=darkblue]Call[/COLOR] CountDown
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
    [COLOR=green]'Yes button (cancel countdown)[/COLOR]
    [COLOR=darkblue]Call[/COLOR] CountDown_Cancel
    Unload Me
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
Put something like below in a standard code module (e.g. Module1) to control the countdown timer. Change MyMacro to the procedure you want to run if the user doesn't click Yes in 10 seconds.
Code:
[COLOR=darkblue]Public[/COLOR] NextTime [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Date[/COLOR]
    
[COLOR=darkblue]Sub[/COLOR] CountDown()
    NextTime = Now + TimeValue("00:00:01")
    UserForm1.Label2.Caption = UserForm1.Label2.Caption - 1
    [COLOR=darkblue]If[/COLOR] UserForm1.Label2.Caption > 0 [COLOR=darkblue]Then[/COLOR]
        Application.OnTime NextTime, "CountDown"
    [COLOR=darkblue]Else[/COLOR]
        Unload UserForm1
        [COLOR=darkblue]Call[/COLOR] [COLOR=#ff0000]MyMacro[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    
[COLOR=darkblue]Sub[/COLOR] CountDown_Cancel()
    Application.OnTime NextTime, "CountDown", Schedule:=[COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 

Forum statistics

Threads
1,081,983
Messages
5,362,551
Members
400,679
Latest member
alecalec202

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top