+ Reply to Thread
Results 1 to 13 of 13

How does the "vbModeless" in "UserForm.show vbModeless" work?

  1. #1
    Registered User
    Join Date
    09-13-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    27

    How does the "vbModeless" in "UserForm.show vbModeless" work?

    Hello,

    How does the "vbModeless" in "UserForm.show vbModeless" work? I have more code after this line, and my guess that it runs the rest of the code until there is none left, at which point the user is free to work on both the worksheet and userform. Am I right? (Before I added "vbModeless", the code would run after I used the userform)

    Now, is there a way to make the program from not continuing to run after that vbModeless is run?


    Thank you!

  2. #2
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    Check the "Show Method" in Excel VBA help. In short, "Modal" prevents any other execution until user input is received; whereas, "Modeless" allows subsequent code to be executed.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    A slight correction (sorry, gyclone)
    Please Login or Register  to view this content.
    If a userform is shown Modally then the calling procedure is suspended until the userform is unloaded or hidden. Once unloaded, the code will resume on the next line after the .SHOW command.

    If shown Modelessly then the userform is loaded/displayed and any code in the INITIALIZE event will execute. However, the calling procedure will resume immediately after the INITIALIZE event (and any other procedures called from it) terminates.

    Showing a userform Modelessly and pausing the code means you will have to rethink the structure of your code... There are ways to do it, but it's difficult to suggest as you have not included any of your existing code.
    Last edited by cytop; 11-12-2013 at 04:43 PM.

  4. #4
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    No need to apologize for clarifying. Here is what is says in the Help file:

    When a UserForm is modal, the user must respond before using any other part of the application. No subsequent code is executed until the UserForm is hidden or unloaded. Although other forms in the application are disabled when a UserForm is displayed, other applications are not.
    I was paraphrasing. Perhaps this is simpler: Modal "pauses" execution until user responds. Modeless does not.

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    OK - between the 2 of us, we covered that fairly comprehensively

    the user must respond before using any other part of the application. No subsequent code is executed until the UserForm is hidden or unloaded.
    Last edited by cytop; 11-12-2013 at 05:03 PM. Reason: Typo

  6. #6
    Registered User
    Join Date
    09-13-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    Thank you gyclone and cytop for your responses!

    What I am trying to do with my code (sub is called "showAddNewEntry") is check if a value (which I enter into an inputbox, "pnstring") exists within the workbook. If it doesn't it comes to this piece of code:

    Please Login or Register  to view this content.
    My userform add an entry to the next available line
    if there is no new entry added, the sub will end itself here
    if there is, then the sub repeats, allowing the user to add another entry

    So, as you can see, because the user hasn't used the userform (and so hasn't added a line) and the code still runs, the else condition is never reached.

    I've tried making the condition >y instead of =y, but then my sub restarts before I even finish with the userform!


    Hope that wasn't too complicated!
    Last edited by aijp; 11-12-2013 at 05:12 PM.

  7. #7
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    I'll need to see the whole sub to be sure of what I'm looking at. But, if I'm following any part of this correctly, the user form will show but the rest of the code is reached before the user has a chance to input anything. If user input is needed/desired, I'd prompt the user and only give them the option of adding or canceling. If they add, do whatever is needed next. If they cancel, exit the sub (or whatever you want to happen).

    if y and the next available are the same, then the user did not add a new entry, suggesting user is done
    Generally not a good idea to rely on your code making assumptions. Give the user 2 (or more) options and explicitly test for the user's choice. Black/white. True/False. 0/1. Etc. Leave no room for error.

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    You are possibly calling the procedure ShowAddNewEntry recursively. This simply means the procedure is calling itself. While this is valid programming construct it should be used carefully and I don't think this is a suitable situation to use recursive calls.

    In my opinion, you need to review the structure of your code... (and that'll be another apology )

  9. #9
    Registered User
    Join Date
    09-13-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    Haha I figured I would! Although before adding the "vbModeless" it was running perfectly!

    If you guys are up for the challenge, I'll leave a copy here (without any entries and many of the sheets, for security reasons).

    I am open to any and all suggestions to improve my code!
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    Perhaps if you explained why you want to show the userform Modelessly...?

    It is used to add new information, so should (my opinion) be modal, blocking access to the worksheets and other elements until entry is complete...

  11. #11
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    I can't download your file at my current location (nothing wrong with the file, just not authorized). I'll try to take a look later tonight, if cytop hasn't already solved. Like cytop, though, I'm curious, if
    before adding the "vbModeless" it was running perfectly
    , what did you hope would happen when you made it modeless?

  12. #12
    Registered User
    Join Date
    09-13-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    First, for a little more context, the programming checks if a PN has already been entered into either worksheet. If it has, it will copy all the information (resourcing package, vendor name/code etc...) associated with that PN

    If it hasn't (aka, a new PN), the userform will pop up. The information for the new PN may be found in another file. I thought it would be easier for the user to be able to copy-paste the required info from the file into the input fields if the userform was modeless

    So, it was working perfectly in the sense that everything was running sequentially, at the time it was supposed to

    Also, in response to an expected reply, no, I cannot easily have a piece of code check if the PN exists in the other file and have all the necessary info copied. The other file, which does not belong to my team, is updated daily and everytime you open it, a _# (# = number of times you opened it that day) is added. On top of it, not everyone needs the file opened. For the sake of coding and user simplicity, it would seem like a hassle to have code for this

    EDIT (kind of)
    ...I'm rereading this last paragraph I wrote and realized perharps I could code it with something like:
    for n = 1 to 10
    'string = file name _n
    'check if open
    'copy values

    'else exit sub open userform


    hmm, i think that could work

  13. #13
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: How does the "vbModeless" in "UserForm.show vbModeless" work?

    Okay, I can see I'm not going to have time to get into your code today. I know what you need, though (at least I think I do). After you launch the entry form (modeless), you need a do/while loop to hold the macro while user either fills in the form and clicks okay, or clicks cancel on the form. Never really dealt with modeless form. You may need to use the form to set a public property. Something like (below not tested or expected to be thorough).

    Please Login or Register  to view this content.
    That's the idea. Modeless allows user to access other file. Do/While prevents code from going any further until user is done. So you get both modeless and modal behavior.

+ 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] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  2. Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)" not working
    By redders in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2011, 03:52 PM
  3. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  4. Replies: 5
    Last Post: 06-26-2006, 09:23 PM
  5. Replies: 7
    Last Post: 05-13-2006, 05:02 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