Userform exit while code is running

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a userform that will automatically close after a set amount of time. My challenge is, that using application.Wait doesn't allow the user to click on a command button to intervene. In this case the command button just closes the workbook immediately instead of waiting. Is there an alternative piece of code that would allow the user to override the wait (similar to ctrl+brk) or this there an alternative to using the wait function to accomplish a timer/countdown?

Thanks!


VBA Code:
Private Sub UserForm_Activate()
    With NEBULA
      .StartUpPosition = 0
      .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
      .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    End With
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.Load_Label.Caption = "Loading Workbook data and settings.."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:04"))
    NEBULA.Load_Label.Caption = "Hiding from Thanos..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:02"))
    NEBULA.Load_Label.Caption = "Preparing the workbook.."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:03"))
    NEBULA.Load_Label.Caption = "Finishing up and opening..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:02"))
    NEBULA.Load_Label.Caption = ""
    NEBULA.Repaint
    
    If Workbooks.Count > 1 Then
    NEBULA.Load_Label.Caption = "I work better alone." & vbNewLine & "Please close any other workbooks and tell Gamora to GET LOST!!!"
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:02"))
    NEBULA.multi_Label.Caption = "Closing workbook in 10 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 9 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 8 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 7 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 6 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 5 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 4 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 3 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 2 seconds..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.multi_Label.Caption = "Closing workbook in 1 second..."
    NEBULA.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    NEBULA.Repaint
    ActiveWorkbook.Saved = True
    Application.Visible = True
    Workbooks("NEBULA").Close
    'Multi.Show
    Exit Sub
    End If
    
    NEBULA.Main_Label.Caption = "Hi There!" & vbNewLine & "What would you like to do?"
    NEBULA.Repaint
    NEBULA.Create_GP.Visible = True
    NEBULA.Close_Program.Visible = True
    NEBULA.Infinity.Visible = True
    
    
    
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try to adapt the following code...

VBA Code:
Option Explicit

Dim abortPauseMacro As Boolean

Private Sub CommandButton1_Click()

    abortPauseMacro = True
    
    'Unload Me
    
End Sub

Private Sub UserForm_Activate()

    'your code here
    '
    '
    
    abortPauseMacro = False
    
    PauseMacro 5
    
End Sub

Private Sub PauseMacro(ByVal secs As Long)

    Dim endTime As Single
    endTime = Timer + secs
    
    Do
        DoEvents
    Loop Until (Timer > endTime) Or (abortPauseMacro = True)
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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