Userform Textbox Error Handling

Jim-in-I

Board Regular
Joined
Sep 10, 2002
Messages
180
I can not figure out how to handle a Userform Texbox input error. The snipet of code in my Command Button macro is:

If UserForm1.TextBox1.Value = "" Then
MsgBox "You must Enter a New Name", vbOKOnly, "Update Error"
Cancel = True
End If

If the user fails to input anything into the textbox, I would like to return to the start of the macro, (let them try it again). Currently, this code just continues running the next macro steps after the user clicks the OK button.
Can anyone provide some guidance as to how I return to the previous step if the Textbox input is blank?

Jim
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I don't know where you have that code, but try something like this:

Code:
    If UserForm1.TextBox1.Value = "" Then
        MsgBox "You must Enter a New Name", vbOKOnly, "Update Error"
        UserForm1.TextBox1.SetFocus
        Exit Sub
    End If
'   More code
 
Upvote 0
Thank you Andrew, you are always so helpful.

I had tried this code variation; however, the Exit Sub removes the Userform. I was hoping to set the focus back to the Userform without exiting, so there is still something wrong in the way I have written this thing.

Jim
 
Upvote 0
As I said I don't know where you had that code. I would expect it to be in the Click Event procedure for a CommandBuuton on your UserForm.
 
Upvote 0

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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