Auto close userform after period of inactivity

Nour

New Member
Joined
Jun 16, 2021
Messages
34
Office Version
  1. 2016
Platform
  1. Windows
Hello house. Pls I have a nightmare. I have a userform which hides my spreadsheet and all entry is via the userform and that is all that users interact with. However I want the userform to show a pop up after 15 min of inactivity with message notifying that it will auto save and close in 2min but if user clicks ok, it should abort the auto exit. Also any event on the userform should prevent the auto pop-up and timing unless inactivity time is reached. Thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Nour.xlsb
caution : minutes are seconds here ;)
in every trigger of your userform, you have to restart the countdown

in thisworkbook
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Stoppen
End Sub
in a module
VBA Code:
Global dTime, bFlag1, bCD

Sub Start()
     Stoppen
     If bFlag1 Then
          dTime = Now + TimeSerial(0, 0, 15)     'now 15 secondes, later 15 minutes !!!
          Application.OnTime dTime, "Start", , 1
          bCD = False
          With UserForm1.TextBox2
               .Text = "testing inactivity during 15 minutes until " & Format(dTime, "hh:mm:ss")
               .BackColor = RGB(0, 255, 0)
          End With
     Else
          If bCD Then     'was busy counting down and is now done
               UserForm1.Hide
               MsgBox "countdown terminated at " & Format(Now, "hh:mm:ss")
          Else
               dTime = Now + TimeSerial(0, 0, 2)     'now 2 seconds, later 2 minutes
               Application.OnTime dTime, "Start", , 1
               With UserForm1.TextBox2     'normally until then Textbox2 isn't visible
                    .Visible = True
                    .Text = "countdown during 2 minutes until " & Format(dTime, "hh:mm:ss")
                    .BackColor = RGB(255, 255, 0)
               End With
               bCD = True     'flag counting down
          End If
     End If

End Sub

Sub Stoppen()
     On Error Resume Next
     Application.OnTime dTime, "Start", , 0
     Application.StatusBar = ""
End Sub

Sub UF1_Show()
     UserForm1.Show
  End Sub
 
Upvote 0

Forum statistics

Threads
1,214,543
Messages
6,120,123
Members
448,947
Latest member
test111

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