Consulting

Results 1 to 13 of 13

Thread: Close a UserForm Via Code Only

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Close a UserForm Via Code Only

    Hello,

    I'm new to User Forms and am working on a simple UserForm that I would like to be able to close via code and not by command button or the title bar "X". Eventually I plan on hiding the UF title bar and not have any buttons on it. I've scoured the net and tried many ways with no success except in the Activate Event such as:

    Private Sub UserForm_Activate()
       Application.Wait Now + TimeValue("00:00:02")
       Unload Me
    End Sub
    However, I do not want to use the activate event or a click event (button) to close it, is there another way?

    Thx
    Gary

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    What would be the trigger for closing it then?
    Be as you wish to seem

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Quote Originally Posted by Aflatoon View Post
    What would be the trigger for closing it then?
    I would like code to close it from an outside sub whether from a standard module or from the user form code. In a standard module I’ve tried the following:

    UserForm1.unload
    UserForm1 unload
    unload UserForm1
    unload.UserForm1

    Ive also tried the above from within the user form code as well as using the Me designation with no success.

    in all cases it either throws an error or it opens up a duplicate user form instead of closing it.

    I’ve also assigned the escape key to close it which works, but only if the user form is activated/in focus. I’m opening up the user form vbModeless.

    I’m beginning to think that it’s just not possible.

  4. #4
    VBAX Regular
    Joined
    Sep 2023
    Posts
    97
    Location
    What I did here was, create a module with a public variable of type UserForm1.
    A button on sheet1 creates a new instance of it and then shows the form.
    A second button on this sheet closes the form using unload.

    Module1
    Public userFormInstance As UserForm1
    Sheet1
    Private Sub btnCloseUserForm_Click()
        
        Unload userFormInstance
    End Sub
    
    
    Private Sub btnDisplayUserForm_Click()
    
    
        Set userFormInstance = New UserForm1
        userFormInstance.Show
        
    End Sub
    EDIT: You can of course use any code to do this, I just placed it in buttons for testing.

  5. #5
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Quote Originally Posted by jdelano View Post
    What I did here was, create a module with a public variable of type UserForm1.
    A button on sheet1 creates a new instance of it and then shows the form.
    A second button on this sheet closes the form using unload.

    Module1
    Public userFormInstance As UserForm1
    Sheet1
    Private Sub btnCloseUserForm_Click()    
        Unload userFormInstance
    End Sub
    
    
    Private Sub btnDisplayUserForm_Click()
    
    
        Set userFormInstance = New UserForm1
        userFormInstance.Show
        
    End Sub
    EDIT: You can of course use any code to do this, I just placed it in buttons for testing.
    Well I'll be dipped in horse hockey, I didn't think I was going to find a solution.
    @jDelano, your code works flawlessly without buttons which was my intent.

    Thank You

  6. #6
    VBAX Regular
    Joined
    Sep 2023
    Posts
    97
    Location
    You're welcome, happy to lend a hand.

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    From horse hockey to nearly presentable to the mother in law...... great work jdelano.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    VBAX Regular
    Joined
    Sep 2023
    Posts
    97
    Location
    Bahahaha

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    That seems unnecessarily complicated (if I'm understanding correctly)

    Simple example of what I was thinking

    3 subs, one loads and shows the UF, one interacts with it, and the last removes it

    Option Explicit
    Dim N As Long
    
    Sub One()
        Load UserForm1
        UserForm1.Show vbModeless
    End Sub
    
    Sub Two()
        N = N + 1
        UserForm1.Label1.Caption = N
    End Sub
    
    Sub Three()
        UserForm1.Hide
        Unload UserForm1
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    That's basically the same principle except you are using the default instance of the form (which is effectively an implicit public variable), which is not really best practice, IMO.
    Be as you wish to seem

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by Aflatoon View Post
    That's basically the same principle except you are using the default instance of the form (which is effectively an implicit public variable), which is not really best practice, IMO.
    How so?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Quote Originally Posted by Paul_Hossler View Post
    That seems unnecessarily complicated (if I'm understanding correctly)

    Simple example of what I was thinking

    3 subs, one loads and shows the UF, one interacts with it, and the last removes it

    Option Explicit
    Dim N As Long
    
    Sub One()
        Load UserForm1
        UserForm1.Show vbModeless
    End Sub
    
    Sub Two()
        N = N + 1
        UserForm1.Label1.Caption = N
    End Sub
    
    Sub Three()
        UserForm1.Hide
        Unload UserForm1
    End Sub
    Paul, I also agree your code is much simpler and easier to understand. I don't know where I went wrong with everything I tried. I couldn't get something very similar to work. Yours works great.

    Thank you
    Gary

  13. #13
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by Paul_Hossler View Post
    How so?
    I'm assuming that relates to my "best practice" comment? If so, then Mathieu says it all here: https://rubberduckvba.blog/2017/10/25/userform1-show/

    Apart from anything else, much like using Dim ... As New ... , you can never test if the object was unloaded/destroyed somewhere by mistake because any reference to it immediately creates a new instance of the object. I've lost count of the number of posts I've seen where people don't understand why reading a userform's control value returns blank when they could see it being input. It usually turns out that the form was unloaded by mistake somewhere but of course using formname.controlname simply reloads the form with its default setup, rather than producing an error, and makes debugging needlessly difficult. To me, it's a bit like not using Option Explicit and then spending hours chasing down bugs due to typos.

    As always, that's not to say I wouldn't do it in quick and dirty one-off code, but I wouldn't use it in production that I expect to see serious use.
    Be as you wish to seem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •