Show userform error message

dan_pafc

Board Regular
Joined
Aug 16, 2007
Messages
162
Hi

Im looking for a bit of code so that if textbox.1 from a userform is left blank when unloading the form, a msg appears with a warning, and doesn’t allow you to save unless all boxes are completed.

I have the following at the moment:

a = TextBox1.Text
If a = "" Then
MsgBox "You must enter your name"
End If
Range("D5").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = a

Kind Regards,

Dan.
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
Put the test in the userform unload routine and don't unload it if it is not completed.
 

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
Yeah, assuming that you have OK and Cancel buttons, then something like

Code:
Private Sub cmdCancel_Click()
    Me.Hide
End Sub

Private Sub cmdOK_Click()
    If Me.TextBox1.Text = "" Then
    
        MsgBox "You must enter your name"
    Else
        .... so your stuff here
        Me.Hide
    End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,304
Messages
5,600,858
Members
414,408
Latest member
macroSmith

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
Top