Stop code execution but keep form open

miicker

Board Regular
Joined
Jun 1, 2014
Messages
75
Hi Everyone,

I have a UserForm where a user can check some check boxes and fill in some field. I have one inputfield where the users enters a number, when the user enters 0, I want a msgbox displayed, and when the user hits "ok" I wan't the form to be still open, but the code to stop executing. I've tried this with an End, Exit Sub, etc. The problem is that the code i'm running is a function, which i'm calling in another piece of code. So when I use "Exit Sub", it still executes the rest of the code.

How do I fix this?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How is this function being executed?
 
Upvote 0
I've never shown a form from a function, until now!

Just tried this (form.show in the function):

Code:
Private Sub TextBox1_AfterUpdate()
    If TextBox1 = "0" Then
        TextBox1 = ""
        TextBox1.SetFocus
        MsgBox "Can't be Zero.", vbInformation, "No no..."
        Exit Sub
    End If
    'Do your other stuff here
End Sub

and it halted the function without a hitch. Is your user form ShowModel set to True? That may have an impact but I haven't tried it.
 
Upvote 0
Thanks for the help, I managed to fix it this way:
Ouside sub:
Code:
Public subfail as Integer
Fisrt sub:
Code:
Sub 1
'some code
Call Sub 2
If subfail = 1 Then
Exit Sub
End If
'more code
End sub
Second sub:
Code:
Sub 2
If 'Some check = False Then
Subfail = 1
Exit Sub
Else
'some code
End If
End sub

This way the form stays opened :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top