+ Reply to Thread
Results 1 to 11 of 11

Programming a UserForm Cancel Button

  1. #1
    Registered User
    Join Date
    11-12-2014
    Location
    USA
    MS-Off Ver
    office 2010 and 2013
    Posts
    40

    Programming a UserForm Cancel Button

    So I have a Userform. I have programmed it to pop up when a user attempts to close the workbook. Here is the code:

    Please Login or Register  to view this content.
    You will see that the code above shows the following command buttons: Save, End, and Cancel. I have managed to figure out how to get the save as dialog box to be displayed when a user clicks on the button SAVE with the code:

    Please Login or Register  to view this content.
    But I am completely and utterly stuck on the button CANCEL. If a user unintentionally or intentionally attempts to close the workbook and decides that he does not want to, how do I get the button CANCEL to stop the action and bring the user back to open workbook. Unload me only closes the userform and then still closes the workbook!

    Can someone help?

    Cheers,
    Kaytie

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

    Re: Programming a UserForm Cancel Button

    Hi Kaytie ,

    Try adding the following line anywhere in Workbook_Before():
    Please Login or Register  to view this content.
    Lewis

  3. #3
    Registered User
    Join Date
    11-12-2014
    Location
    USA
    MS-Off Ver
    office 2010 and 2013
    Posts
    40

    Re: Programming a UserForm Cancel Button

    Thanks for answering my post, LJMetzger. So this is code I have:

    Please Login or Register  to view this content.
    Now what is the code that I need to use to program the END command button to save the file at the last known save location and close out the workbook altogether? Can you help me with this one too, LJMetzger? Anyone else willing to answer?

    Thanks!

    ~Kaytie

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Programming a UserForm Cancel Button

    Please don't cross-post without adding links.

    http://www.mrexcel.com/forum/excel-q...el-button.html
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    11-12-2014
    Location
    USA
    MS-Off Ver
    office 2010 and 2013
    Posts
    40

    Re: Programming a UserForm Cancel Button

    So, sorry! My brain is so busy trying to get this done that it slipped my mind to come back and post the cross-post. Don't throw the book at me!

  6. #6
    Registered User
    Join Date
    11-12-2014
    Location
    USA
    MS-Off Ver
    office 2010 and 2013
    Posts
    40

    Re: Programming a UserForm Cancel Button

    You all are killing me slowly! Why can I not figure this out for the life of me! Please, please someone tell me how to program a command code to save the workbook at its last known location and file name and close out excel. My brain is hurting and I want to scream in frustration!

    Kaytie

  7. #7
    Registered User
    Join Date
    11-12-2014
    Location
    USA
    MS-Off Ver
    office 2010 and 2013
    Posts
    40

    Re: Programming a UserForm Cancel Button

    So this is the code I am using for the END button:

    Please Login or Register  to view this content.
    And I get the following popup window from Visual Basic: "Run-time error '400: Form already displayed; can't show modally". I click on the DEBUG button and I am routed to the following code:

    Please Login or Register  to view this content.
    The line "frmUser.Show" is highlighted yellow. What the heck am I doing wrong?

    ~Kaytie

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Programming a UserForm Cancel Button

    Kaytie

    I think you need to explain exactly what you are trying to do, maybe even upload an example workbook.

    For example, what's the purpose of the userform?

  9. #9
    Registered User
    Join Date
    11-12-2014
    Location
    USA
    MS-Off Ver
    office 2010 and 2013
    Posts
    40

    Re: Programming a UserForm Cancel Button

    Yeah. No problem.

    The purpose of the userform is to create a user log of the excel workbook. So when a user opens up the spreadsheet, he lands on Sheet1 entitled Instructions. Hopefully I can figure out a way to hide subsequent sheets. Once the user reads through the instructions, the user clicks on the command button entitled "Click here to display sheets in this workbook.". This opens up the userform. The user then inputs her first name, last name, and her employee's id. Then the user clicks on the START button. This inputs the information on Sheet5 and date and times stamp the information. The form closes. Here is the code for this process. So far it work spectacularly well thanks to Jim882.

    Please Login or Register  to view this content.
    Now I am working on the end. When the user is done with the review and attempts to close out the workbook, the userform is displayed with buttons to SAVE, END, and CANCEL. The user would use the CANCEL command button if he unintentionally or intentionally quits excel and either didn't want to or changes his mind. The SAVE command button displays excel's Save As dialogue box so they can save it where ever they want to on their computer. And then if the user selects the command button END, then I want excel to auto save the workbook, place a date and time stamp in Sheet5 Column F which is along side the date and time stamp when the user began her work, then close excel. The code that people have helped me come up with so far is:

    Please Login or Register  to view this content.
    The entire file is attached. Please, I am begging..... I NEED HELP WITH THIS. I am spinning my wheels here. Imagine me on a stage, on my knees begging the audience (you all). LOL

    Cheers,
    Kaytie
    Attached Files Attached Files

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

    Re: Programming a UserForm Cancel Button

    Hi Kaytie,

    Try the following code (significant changes in red):
    In the ThisWorkbook Module:
    Please Login or Register  to view this content.

    In the UserForm Module:
    Please Login or Register  to view this content.
    Last edited by LJMetzger; 11-18-2014 at 09:16 AM.

  11. #11
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Programming a UserForm Cancel Button

    PLease see attached file

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    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. [SOLVED] exit sub when press cancel button on userform
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2014, 01:13 AM
  2. Help for Code for Cancel Button on Userform.
    By allens12 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-16-2014, 11:32 PM
  3. issues with userform cancel button
    By jw01 in forum Excel General
    Replies: 0
    Last Post: 12-29-2010, 05:28 PM
  4. How do I disable the cancel button for a userform?
    By Evalis in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-14-2010, 04:14 PM
  5. userform cancel button
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-12-2006, 12:30 PM

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