Saturday, 15 September 2012

vba - Application.Quit in UserForm attempts to run rest of macro before exiting -



vba - Application.Quit in UserForm attempts to run rest of macro before exiting -

my question is: using vba in excel 2013 how can gracefully close entire instance of excel when user decides don't want fill out userform , clicks quit or cancel?

currently, if user clicks quit or cancel, check see if instance 1 open. if not, can utilize thisworkbook.close , think okay. however, if is, not want application still present, used application.quit. this, though, tries finish running macro, throws errors (originally "type mismatch" because unload form), , closes after click "debug" or "end" (which fast either cannot debug). i'm ignoring first case , trying exit entire application. it's long macro lot of subroutines , functions, debugging , posting here, have shortened it. type mismatch error no longer occurs, believe consequence of actual error: code running after command close application called.

first, here's code starts everything:

private sub commandbutton1_click() phone call form_variables frm_request.show phone call a_request_main end sub

the subroutine

form_variables

is subroutine creates public variables can store info userform.

frm_request.show

initializes (including calling function finds workbook, extracts list, formatting, closes workbook , enters list userforms drop downwards box) , shows form, , finally

a_request_main

uses public variables (where userform info stored) , thing (but shouldn't if userform closed).

the code executed when .show called is:

private sub userform_initialize() ' job numbers other workbook dim job_selection_list variant job_selection_list = get_job_list() frm_request.job_number_combobox .list = job_selection_list end ' set focus on job numbers jn_combobox.setfocus end sub private sub cancel_button_click() set job_selection_list = nil unload me application.quit end sub private sub submit_button_click() ' values userform saved global (?) variables other subroutines can access. end sub

i stepped through programme , saw that, 1 time application.quit called in userform, macro, in main subroutine, steps

call a_request_main

but should close out. tried doing "save" commands, , changing order of things, , read objects needing set nil (hence setting of job_selection_list created when drop downwards list initialized), cannot seem work, or find online. can provide guidance or allow me know of improve way close excel instance? help me stack-overflow kenobi, you're hope!

thanks.

just add together variable business relationship when user closes form

in form

'hold flag if users cancels form public btncancel boolean private sub commandbutton1_click() unload me btncancel = true end sub 'set flag each time form active private sub userform_activate() btncancel = false end sub

then in code

call form_variables frm_request.show if frm_request.btncancel application.quit else phone call a_request_main end if

vba excel-vba userform quit

No comments:

Post a Comment