Userform problem

Roccafelie

Board Regular
Joined
Jun 30, 2008
Messages
129
Hello Everyone

I am looking for a macro or code that if a field not filled or choosen in userform, there will be a error message says "cant continue, please make sure all fields are filled etc..)

I believe it s asked before, but I cant find the relevant thread.

I ll be hapy, if you could give me a hand with that

Thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Example:

Code:
Private Sub CommandButton1_Click()
    If TextBox1.Text = "" Then
        MsgBox "Please complete TextBox1"
        TextBox1.SetFocus
    Else
'       Code if all OK
    End If
End Sub
 

Roccafelie

Board Regular
Joined
Jun 30, 2008
Messages
129
Private Sub CommandButton1_Click()
If TextBox1.Text = "" Then
MsgBox "Please complete TextBox1"
TextBox1.SetFocus
Else
' Code if all OK
End If
End Sub


but this is only for textbox1 ot filled, what if there are more than 1 text boxes, will it be like this ?

Private Sub CommandButton1_Click()
If TextBox1.Text = "" Then
MsgBox "Please complete TextBox1"
TextBox1.SetFocus
If TextBox2.text = "" Then
MsgBox "Please complete TextBox2"
TextBox1.SetFocus
Else
' Code if all OK
End If
End Sub

?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092

ADVERTISEMENT

Like this:

Code:
Private Sub CommandButton1_Click()
    If TextBox1.Text = "" Then
        MsgBox "Please complete TextBox1"
        TextBox1.SetFocus
    ElseIf TextBox2.Text = "" Then
        MsgBox "Please complete TextBox2"
        TextBox2.SetFocus
    Else
'       Code if all OK
    End If
End Sub
 

Roccafelie

Board Regular
Joined
Jun 30, 2008
Messages
129

ADVERTISEMENT

what if an optionbutton not choosen ?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
If an OptionButton is not checked its value is False. Example:

Code:
Private Sub CommandButton1_Click()
    If OptionButton1.Value = False Then
        If OptionButton2.Value = False Then
            If OptionButton3.Value = False Then
                MsgBox "Please choose an option"
                Exit Sub
            End If
        End If
    End If
'   Code if one OptionButton checked
End Sub
 

Roccafelie

Board Regular
Joined
Jun 30, 2008
Messages
129
I coulnt adjust this macro to the one above

I need to use "else if" function
 

Roccafelie

Board Regular
Joined
Jun 30, 2008
Messages
129
maybe writing a new macro considering their Groupname
e.g if none of the option buttons belongs to Group1 are not chosen then MsgBox "Please choose an option"
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,029
Messages
5,835,036
Members
430,333
Latest member
jaypatel1298

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