Results 1 to 6 of 6

Thread: Stopping code from inside a userform??
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jun 2002
    Posts
    964
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Stopping code from inside a userform??

    My code calls a userform within which I placed a "Cancel" button intended to stop all code. However, I can't figure out how to make that cancel button actually stop the code. I tried a global boolean but despite being set true in the userform form, it stayed set as false in the code that called the userform. I guess the actual question is how do I pass values from a userform back to the calling code.

    I'm at a loss...

    Thanks in advance
    Roscoe

  2. #2
    Board Regular
    Join Date
    Jul 2012
    Location
    Hampshire, UK
    Posts
    4,771
    Post Thanks / Like
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Stopping code from inside a userform??

    Hi,
    Where have you placed the global boolean variable - if it is in your forms code page you need to place it in a standard module.

    If still having issues, always helpful to forum if you can publish code having problems with.

    Hope Helpful

    Dave

  3. #3
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    33,479
    Post Thanks / Like
    Mentioned
    46 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Stopping code from inside a userform??

    Set a property of the form, then hide it (do not unload it) and then read the property in the calling code.

  4. #4
    Board Regular jmacleary's Avatar
    Join Date
    Oct 2015
    Location
    at a desk in the UK
    Posts
    524
    Post Thanks / Like
    Mentioned
    12 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Stopping code from inside a userform??

    Hi there. A global should work. I knocked up a quick bit of code. First this in Module 1:
    Code:
    Global stopit As Boolean
    Sub startup()
    stopit = vbFalse
    UserForm1.Show
    If stopit = vbTrue Then
        MsgBox "stop now"  ' you could put exit sub here
    Else
        MsgBox "do some more stuff"
    End If
    End Sub
    and this in the userform for the cancel button:
    Code:
    Private Sub CommandButton1_Click()
    stopit = vbTrue
    Unload Me
    End Sub
    when I ran the subroutine and clicked the button, I got the message "stop now" which meant the global had taken the value. Hope this helps.

    John
    If my answer has helped, please vote using the Thanks or Like buttons on the left.
    John

  5. #5
    Board Regular
    Join Date
    Mar 2015
    Posts
    3,194
    Post Thanks / Like
    Mentioned
    56 Post(s)
    Tagged
    6 Thread(s)

    Default Re: Stopping code from inside a userform??

    You can cancel code with
    Code:
      End
    BUT that may cause you some other issues
    eg variable values are lost - but those values can be preserved by dump to worksheet immediately before the code is terminated

    Rather than using the End function, a more controlled exit is preferable (I think that is what you are in fact attempting)

    If none of the other suggestions (offered in earlier posts above) have solved your problem, explain more fully exactly what you are trying to do ...
    1. What is the code doing when you want it stopped?
    2. Why do you want it stopped abruptly?
    3. Should any variable values to be retained when the code stops?
    Last edited by Yongle; Apr 17th, 2019 at 04:18 AM.

  6. #6
    Board Regular
    Join Date
    Jun 2002
    Posts
    964
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Stopping code from inside a userform??

    Quote Originally Posted by jmacleary View Post
    Hi there. A global should work. I knocked up a quick bit of code. First this in Module 1:
    Code:
    Global stopit As Boolean
    Sub startup()
    stopit = vbFalse
    UserForm1.Show
    If stopit = vbTrue Then
        MsgBox "stop now"  ' you could put exit sub here
    Else
        MsgBox "do some more stuff"
    End If
    End Sub
    and this in the userform for the cancel button:
    Code:
    Private Sub CommandButton1_Click()
    stopit = vbTrue
    Unload Me
    End Sub
    when I ran the subroutine and clicked the button, I got the message "stop now" which meant the global had taken the value. Hope this helps.

    John
    That's what I thought I did as well but the boolean still showed as false after unloading the form. I found a less elegant workaround that suits me for now. If I find the time and motivation I'll try this again.

    Thanks
    Roscoe

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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