+ Reply to Thread
Results 1 to 2 of 2

how to close userform using commndbutton and set automatic close at certain time

  1. #1
    Registered User
    Join Date
    12-22-2014
    Location
    kuala lumpur,malaysia
    MS-Off Ver
    2010
    Posts
    1

    how to close userform using commndbutton and set automatic close at certain time

    hi,
    i have a userform and i want to set it if user click close, it will unload the userform but if user didnt close or the userform is idle for 1min, the userform will automatically close.

    i have try this

    Private Sub UserForm_Activate()
    startTimer = Timer

    Do While Timer < (startTimer + 60)
    DoEvents
    Loop
    Unload Me

    End Sub


    and at every button in userform i try this

    Private Sub ComboBoxx4_Change()
    startTimer = Timer
    End sub


    Sub CommandButton1_Click()
    'close button
    startTimer = Timer
    Unload Me
    End Sub


    but the codes doesnt work.it will close in 1 min even the userform is not idle.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: how to close userform using commndbutton and set automatic close at certain time

    Hi fatinmunie and welcome to ExcelForum,

    You were very close. Your 'startTimer' needs to be a global variable. In your code, each Sub is isolated from each other, so UserForm_Activate() never was able to get the new value of startTimer from the other routines.

    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    See the attached sample workbook (20 second timeout because I am impatient) and the code that follows below.

    In the UserForm1 module:
    Please Login or Register  to view this content.
    Ordinary Code Module:
    Please Login or Register  to view this content.
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    I answered your question, because this was your first post.

    Lewis
    Attached Files Attached Files

+ 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. Disable close workbook but still force users to close thru command button
    By rathig in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2013, 07:29 AM
  2. Code for automatic close and save after lap of time
    By Raphski in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-27-2012, 09:24 AM
  3. [SOLVED] Real Time Clock Encountering Error Upon Userform Close
    By Stonesteel15 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2012, 11:51 PM
  4. Help with automatic save and close after set time
    By redstang423 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-03-2006, 02:25 AM
  5. [SOLVED] Excel shoud not close all active books when clicking close button
    By technomike in forum Excel General
    Replies: 0
    Last Post: 06-10-2005, 01:05 PM

Tags for this Thread

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