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
 
Combined:

Code:
Private Sub CommandButton1_Click()
    If TextBox1.Text = "" Then
        MsgBox "Please complete TextBox1"
        TextBox1.SetFocus
        Exit Sub
    ElseIf TextBox2.Text = "" Then
        MsgBox "Please complete TextBox2"
        TextBox2.SetFocus
        Exit Sub
    ElseIf 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
    Else
'       Code if all OK
    End If
End Sub
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Expanded (with no compile errors):

Code:
Private Sub CommandButton1_Click()
    If TextBox1.Text = "" Then
        MsgBox "Please complete TextBox1"
        TextBox1.SetFocus
        Exit Sub
    ElseIf TextBox2.Text = "" Then
        MsgBox "Please complete TextBox2"
        TextBox2.SetFocus
        Exit Sub
    ElseIf 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
    ElseIf ComboBox1.ListIndex = -1 Then
        MsgBox "Please selct from the ComboBox"
        Exit Sub
    Else
'       Code if all OK
    End If
End Sub
 
Upvote 0
I got the message but the userform still sends the email:

Private Sub Enviar_Click()
Dim OutApp As Object
Dim OutMail As Object


Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)


With UserForm1
cbx1 = .CheckBox1.Caption
cbx2 = .CheckBox2.Caption
txtbox1 = .TextBox1
txtbox2 = .TextBox2
End With


On Error Resume Next
With OutMail
.To = "antonio.rodrigues@rangel.pt"
.CC = ""
.BCC = ""
If cbx1.Value = True Then
.Subject = cbx1
On Error Resume Next
End If
If cbx1.Value = False Then
.Subject = cbx2
On Error Resume Next
End If
If cbx2.Value = True Then
.Subject = cbx2
End If
If cbx2.Value = False Then
.Subject = cbx1
End If
On Error Resume Next
.HTMLBody = "SL ERROR - " & vbcrf & txtbox1 & " " & vbcrf & vbcrf & txtbox2
.Send
End With
If TextBox1.Text = "" Then
MsgBox "Por favor complete o formulário"
TextBox1.SetFocus
Unload UserForm1

ElseIf TextBox2.Text = "" Then
MsgBox "Por favor complete o formulário"
TextBox2.SetFocus
Unload UserForm1
End If

On Error GoTo 0


Set OutMail = Nothing
Set OutApp = Nothing


Unload UserForm1
End Sub

Anybodi hel?
 
Upvote 0
Why have you posted your question on a thread that is over 4 years old and that doesn't relate to your problem?
 
Upvote 0
Sorry, not very good at these forum things. Just looked at the date now. Part of the question does relate because I do need it to display a message saying "Please complete the userform." And does pop ub the message but I thought the entire hole point of the message was not to allow the userform to work regardless of what it is supposed to do.
 
Upvote 0
You have your tests in the wrong place. Try:

Rich (BB code):
Private Sub Enviar_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    If TextBox1.Text = "" Then
        MsgBox "Por favor complete o formulário"
        TextBox1.SetFocus
        Exit Sub
    ElseIf TextBox2.Text = "" Then
        MsgBox "Por favor complete o formulário"
        TextBox2.SetFocus
        exitsub
    End If
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    With UserForm1
        cbx1 = .CheckBox1.Caption
        cbx2 = .CheckBox2.Caption
        txtbox1 = .TextBox1
        txtbox2 = .TextBox2
    End With
    On Error Resume Next
    With OutMail
        .To = "antonio.rodrigues@rangel.pt"
        .CC = ""
        .BCC = ""
        If cbx1.Value = True Then
            .Subject = cbx1
            On Error Resume Next
        End If
        If cbx1.Value = False Then
            .Subject = cbx2
            On Error Resume Next
        End If
        If cbx2.Value = True Then
            .Subject = cbx2
        End If
        If cbx2.Value = False Then
            .Subject = cbx1
        End If
        On Error Resume Next
        .HTMLBody = "SL ERROR - " & vbcrf & txtbox1 & " " & vbcrf & vbcrf & txtbox2
        .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    Unload UserForm1
End Sub
 
Upvote 0
Still not working. The exitsub gave me an error because it was together so i changed to exit sub but it still does the same thing. Thank you for the help anyways. If you have any other ideas I would appreciate the help!
 
Upvote 0
If the textboxes are not completed it gives out the message but it doesnt work with the checkboxes. The exitsub gave me an error because it was together so i changed to exit sub but it still does the same thing. I've been experimenting with the code below but so far nothing. Thank you for the help anyways. If you have any other ideas I would appreciate the help!

Private Sub CommandButton1_Click()
Unload UserForm1
End Sub
Private Sub Enviar_Click()
Dim OutApp As Object
Dim OutMail As Object

If TextBox1.Text = "" Then
MsgBox "Por favor complete o formulário"
TextBox1.SetFocus
Exit Sub
End If

If TextBox2.Text = "" Then
MsgBox "Por favor complete o formulário"
TextBox2.SetFocus
Exit Sub
End If

If CheckBox1.Caption = "" Then
MsgBox "Por favor complete o formulário"
CheckBox1.SetFocus
Exit Sub
End If

If CheckBox2.Caption = "" Then
MsgBox "Por favor complete o formulário"
CheckBox2.SetFocus
Exit Sub
End If

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With UserForm1
cbx1 = .CheckBox1.Caption
cbx2 = .CheckBox2.Caption
txtbox1 = .TextBox1
txtbox2 = .TextBox2
End With
On Error Resume Next
With OutMail
.To = "antonio.rodrigues@rangel.pt"
.CC = ""
.BCC = ""
On Error Resume Next
.HTMLBody = "SL ERROR - " & vbcrf & txtbox1 & " " & vbcrf & vbcrf & txtbox2
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Unload UserForm1
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

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