Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Userform exit macro

This is a discussion on Userform exit macro within the Excel Questions forums, part of the Question Forums category; Hi all, I a macro (Main) I start a userform (FrmMenu.show). This userform has a cancel button (CmdCancel). When the ...

  1. #1
    Board Regular
    Join Date
    Jun 2002
    Location
    Deventer
    Posts
    372

    Default Userform exit macro

    Hi all,

    I a macro (Main) I start a userform (FrmMenu.show).
    This userform has a cancel button (CmdCancel). When the cancel button is activated I want the macro (Main) to stop running.

    Any idea.

    JH

  2. #2
    Board Regular texasalynn's Avatar
    Join Date
    May 2002
    Location
    Houston, TX
    Posts
    8,276

    Default

    put this code for the button (change the Formx to match yours)
    Unload UserForm1

  3. #3
    Board Regular
    Join Date
    Jun 2002
    Location
    Deventer
    Posts
    372

    Default

    The code used in the userform for the cancel button is:

    Private Sub CmdCancel_Click()
    Unload Me
    End Sub

    Now it goes back to the Main macro. And the Main macro it want to stop.

  4. #4
    MrExcel MVP
    Moderator
    Smitty's Avatar
    Join Date
    May 2003
    Location
    Crested Butte, CO
    Posts
    27,104

    Default

    Have you tried Exit Sub?

    Smitty
    Smitty

    Every once in a while, there's a sudden gust of gravity...

    Check out my new book at the Mr. Excel Bookstore!

    Mr. Excel HTML Maker - Post a shot of your sheet

  5. #5
    Board Regular
    Join Date
    Jun 2002
    Location
    Deventer
    Posts
    372

    Default

    Yes. I treid Exit Sub before and after the Unload Me.
    But this doesn't work.

  6. #6
    Board Regular texasalynn's Avatar
    Join Date
    May 2002
    Location
    Houston, TX
    Posts
    8,276

    Default

    show the code you have for the "MAIN" macro

  7. #7
    MrExcel MVP HalfAce's Avatar
    Join Date
    Apr 2003
    Location
    Alaska
    Posts
    9,230

    Default

    You can use public variable for this.
    Here's an example.
    In a standard module (where "Main" code is if it's in one) put this at the very top of the
    module:
    Code:
    Public Halted As Boolean
    Then to modify your existing "Main" code you can do something like this.
    Code:
    Sub Main()
    'At the beginning of "Main" macro code
    Halted = 0
    
    'Continue your "Main" macro code with what's supposed _
     to execute before the FrmMenu gets called.
    
    'Just above the point at which you want the code _
     execution to stop if Cancel has been pressed. . .
    If Halted <> 0 Then Exit Sub
    
    'The remainder of your "Main" macro code
    
    End Sub
    Short of this helping then as texasalynn suggests, perhaps you can post your existing
    code and we can help from there.

    Hope it helps.

  8. #8
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    65,670

    Default

    If you want to stop all code use End.
    If posting code please use code tags.

  9. #9
    MrExcel MVP erik.van.geit's Avatar
    Join Date
    Feb 2003
    Location
    Belgium 3272 Testelt
    Posts
    17,765

    Default

    You can use public variable for this.
    Here's an example.
    In a standard module (where "Main" code is if it's in one) put this at the very top of the
    module:
    Code:
    Public Halted As Boolean
    Then to modify your existing "Main" code you can do something like this.
    Code:
    Sub Main()
    'At the beginning of "Main" macro code
    Halted = 0
    
    'Continue your "Main" macro code with what's supposed _
     to execute before the FrmMenu gets called.
    
    'Just above the point at which you want the code _
     execution to stop if Cancel has been pressed. . .
    If Halted <> 0 Then Exit Sub
    
    'The remainder of your "Main" macro code
    
    End Sub
    Short of this helping then as texasalynn suggests, perhaps you can post your existing
    code and we can help from there.

    Hope it helps.
    Hi, all,

    Dan, you forgot the userform part
    Code:
    Private Sub CmdCancel_Click() 
    Halted = 1 'or True
    Unload Me 
    End Sub
    or did I miss something ?

    kind regards,
    Erik

  10. #10
    MrExcel MVP HalfAce's Avatar
    Join Date
    Apr 2003
    Location
    Alaska
    Posts
    9,230

    Default

    Dan, you forgot the userform part
    Code:
    Private Sub CmdCancel_Click()
    Halted = 1 'or True
    Unload Me
    End Sub

    or did I miss something ?
    Nope, you didn't miss anything. You're absolutely right.
    I just plain forgot to include that part.
    I guess it would be kind of important wouldn't it.

    Good thing you're keepin' an eye on me.

Page 1 of 2 12 LastLast

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
  •  


DMCA.com