Force Code To Wait Until Userform Is Closed

    • #1

    Situation:
    At two places in the middle of my code I need to specify a particular variable; usually this is done by some autodection-method, but whenever this fails a userform gets loaded and the user needs to do some clicks which finally specifies that variable.

    Problem:
    So far my code does NOT wait for the userform (and the user-input), but continues running until it fails, cause that particular variable (see above) had not been initialised with the correct value for this run.

    My Question:
    How can I force the code to wait until the userform has been closed? Or what other way do you usually solve that problem when wanting to use a userform in mid-code? Below you find the place where I load the User form 'UsrFormSpecifyFormat'.

    Have a pleasant sunday,
    Andy

    • #2

    Re: Force Code To Wait Until Userform Is Closed

    Hi Andy,

    You would have to set the output of the form to the variable and test if anything was entered...similar to the example below where you set a variable to be an inputbox. If the user clicks "cancel" then the variable returned = false. You can then check its condition and run code accordingly.

    Does that help?

    Brad

    • #3

    Re: Force Code To Wait Until Userform Is Closed

    Hi Brad,

    thank you for your input! Excuse me for asking, but how do I set the output of the form to the variable, then? As far as I have tried it is not possible to do something like 'x=userform.show', as an userform does not have an output.

    I have tried your code ... and with that input-box it works perfectly fine with me :), it's just that I need it to work with an userform instead of an inputbox...

    Andy

    • #4

    Re: Force Code To Wait Until Userform Is Closed

    Hi Andy,

    I'd need a little more detail as to what's happening in your code. In most situations I would validate the userforms data before letting them close the form. Eg, If the information isn't right, print an error message and don't hide the form.

    Can you post a little more of the code and what is happening with it?

    Brad.

    • #5

    Re: Force Code To Wait Until Userform Is Closed

    hey, sure! And thanks for the quick answer.

    How much ... or what code do you need? I'll insert here my userform-code (see below)

    Andy

    Purpose of UserForm:
    The purpose of the userform is to let the user chose between up to 7 different input-data-formats. (This selection is supported with some graphical feature like highlighting certain cells according to the chosen format as well as with some information about the availability of certain formats.) Finally, the user's choice will be stored as an integer [1..7] in a globally visible variable called "slotNumber" of type integer.

    Structure of UserForm:
    The UserFrom contains 7 option buttons [Thanks to Tony Ward;)] and two command buttons: one for accepting the chosen format, the other one for cancelling and returning. The former one also includes a data-validation-function, returning 'true' if user-input can be processed.

    Code of the UserForm:

    • #6

    Re: Force Code To Wait Until Userform Is Closed

    Do you have a main procedure that calls (or shows) the form?

    Brad.

    • #7

    Re: Force Code To Wait Until Userform Is Closed

    OK, then this is the sub from where the userform gets called. This userinput is needed in the second but last 'Select Case'-loop for the last 'Select Case'-loop to execute correctly.

    Most of the constants and variables are not needed in this case here, I guess, but the main-program with all its subs requires several.

    :D

    • #8

    Re: Force Code To Wait Until Userform Is Closed

    Hi Andy,

    Are you able to attach the workbook or a sample of it. I'm having a little trouble wrapping my head around what this code is doing.

    Generally speaking, I would have the form wrapping around the select case rather than having the form inside the select case statement. The form would then trigger another procedure which could pass information to the main procedure or the public variables.

    Brad.

    • #9

    Re: Force Code To Wait Until Userform Is Closed

    ai, so you mean, at the end of my sub I should launch/show the user form in any case, but have that 'Select Case'-loop WITHIN the user form? Wow, that's great, I shall give it a try! Thank you! :)

    Due to some extensions, currently, the workbook is one single contruction site with about 50 subs, and only half of them is tuned into a general concept. ;) I will see if I get your idea working, if not ... well, then I will paste here some running mini-version of the code.

    • #10

    Re: Force Code To Wait Until Userform Is Closed

    Quote from Andy Hennig

    Due to some extensions, currently, the workbook is one single contruction site with about 50 subs, and only half of them is tuned into a general concept. ;) I will see if I get your idea working, if not ... well, then I will paste here some running mini-version of the code.


    I understand Andy. Let me know how you go. It just seems a bit odd that the form is within the code and not the other way around. Generally, forms are the starting point for running a procedure.

    Anyway, I'd be interested to know how you go. There are so many ways to address one single problem within excel, we'll get a workable solution eventually.

    Brad

    • #11

    Re: Force Code To Wait Until Userform Is Closed

    You've opened your form "modeless".

    [vba]
    UsrFormSpecifyFormat.Show vbModeless
    [/vba]

    Here's an excerpt from http://www.thecodenet.com/articles.php?id=7

    HTH

    • #12

    Re: Force Code To Wait Until Userform Is Closed

    :) Oh, great, it worked out both ways! First I tried to solve the problem the way bradles suggested, then the way you suggested it. The latter seems to be easier to implement, this is why I'll go that way.

    For all those experiencing a simillar problem, wanting to go the other way, this is the solution that bradles recommended and worked with me, too:
    a) right at the end of my code I place the load command for the user form. Note: I load it but I do not show it.


    b) Into the Userform Sub 'UserForm_Initialize()' I placed the 'decision-making' code (with me it was a 'Select Case'-statement), that either calls another sub OR shows the userform so the user can interact with the userform itself.

                                                                                        Dim intVariable                                                                                                                    Select case intVariable                                                                                                                                                    Case 0                                                                                                                                                    Call someSub                                                                                                                                                    Case 1                                                                                                                                                    Call anotherSub                                                                                                                                                    Case Else                                                                                                                                                    UserForm1.Show                                                                                                                    End Select                                                                              

    Just make sure you don't have no code like stated below BEFORE the proper and full execution of 'UserForm_Initialize()' has come to an end.