+ Reply to Thread
Results 1 to 11 of 11

Correctly close Excel from within a userform code. Is there a way?

  1. #1
    Registered User
    Join Date
    08-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    30

    Correctly close Excel from within a userform code. Is there a way?

    I'm running a small example (attached) of using userforms to collect data and perform some calculations, and worksheets to display the results. After exiting from application an instance of Excel still running as a background process, and needs ending from the Task Manager. I understand that trying to close Excel from within a userform is controversial but I can't figure out how to otherwise do it. Any help, please?
    Attached Files Attached Files

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Correctly close Excel from within a userform code. Is there a way?

    Hello florin_excel,

    It is unclear to me what your objectives with the selection of Variables are.

    Your Code had to many errors to show here, as it will take up a lot of space.

    The attached sample Workbook contains code that will solve your issue, by closing the Workbook, and leave you the option of quitting Excel manually, since you may have other Workbooks open at the same time.

    Regards.
    Attached Files Attached Files
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Registered User
    Join Date
    08-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Correctly close Excel from within a userform code. Is there a way?

    Thank you for looking into this, Winon.

    My intention with this exercise was to understand if Excel can be properly closed from within the vba code, so I didn't pay much attention to the rest of it, and I assume I didn't make it clear what the intended functionality was; apologies. So, the user can choose between two tasks, perform some calculations within that task and display the results on the nominated sheet for that task. Excel sheets should only be visible for displaying the results, while using the Exit button in the "MainMenu" userform should close the application.

    I've tried the file you've kindly sent but it doesn't display Sheet1 or Sheet2 after using Task1 or Task2, respectively, only briefly flashes on the screen Sheet3 while changing tasks. Moreover, giving the user the job of closing an empty Excel window after closing the app isn't very appealing.


    Kind regards.

  4. #4
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,491

    Re: Correctly close Excel from within a userform code. Is there a way?

    you may want to add these lines to the Exit button code

    Please Login or Register  to view this content.
    that will end Excel without warning messages to save changes

  5. #5
    Registered User
    Join Date
    08-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Correctly close Excel from within a userform code. Is there a way?

    Hi Roel, thank you for your reply.

    The only problem with the suggested approach is that it will close, without warning or saving, any other opened Excel workbooks the user might have been working on.
    Is there a way to circumvent this?

    Regards.

  6. #6
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,491

    Re: Correctly close Excel from within a userform code. Is there a way?

    remove the displayalerts = false line then..

    But then the user can be confronted with a lot of messages to save and then it becomes "dictator code" forcing the user to close all its workbook just because you want a nice clean exit for your sheet.

    I think you will have to look for a solution where you properly unload the form. so that you get back to the open workbook and than trigger an eventcode to close the workbook. then there should also be no problem with the never ending instance of excel..

    So then the answer to your topic title would be. that you cannot close excel from a userform. you will have to handle proper closing thru the workbook events. But that should not be a big issue, you should still be able to keep the workbook minimized / hardly visible to the user.
    Last edited by Roel Jongman; 03-07-2019 at 07:18 AM.

  7. #7
    Registered User
    Join Date
    08-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Correctly close Excel from within a userform code. Is there a way?

    Thank you, Roel. Your last paragraph is a clear answer to my question.

    Regards.

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Correctly close Excel from within a userform code. Is there a way?

    Thank you for the feedback florin_excel,

    giving the user the job of closing an empty Excel window after closing the app isn't very appealing.
    Yes, I did explain that in my Post to you;
    ....leave you the option of quitting Excel manually, since you may have other Workbooks open at the same time.
    The attached sample should work now with task1 and task2.

    Regards.
    Attached Files Attached Files
    Last edited by Winon; 03-07-2019 at 01:05 PM. Reason: Attached correct Workbook.

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Correctly close Excel from within a userform code. Is there a way?

    Hi florin_excel,

    In Post # 8 I have attached the incorrect Workbook, and edited the Post to reflect the correct Workbook.

    Regards.

  10. #10
    Registered User
    Join Date
    08-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Correctly close Excel from within a userform code. Is there a way?

    Thank you Winon, I appreciate your help.
    Regards.

  11. #11
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Correctly close Excel from within a userform code. Is there a way?

    Hi florin_excel,

    You are welcome, glad we could help. Also, thank you for the Rep+, I appreciate it!

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Thanks.

+ 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. Replies: 1
    Last Post: 03-23-2018, 03:52 PM
  2. VBA code - Userform not to close
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-15-2016, 01:21 PM
  3. [SOLVED] execute code on userform close buton
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-10-2016, 02:22 PM
  4. Code for save and close Button on Userform
    By cjames2012 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-16-2013, 12:49 AM
  5. userform button code needs attention to work correctly
    By rampres! in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-15-2012, 03:30 PM
  6. Code a Command Button on UserForm not to close upon click - Excel VBA
    By eemiller1997 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-14-2012, 02:20 PM
  7. Dissable Close button on Userform or Assign Code
    By RobynC in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-19-2007, 10:07 AM

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