msgBox VBA Help needed

josnow

New Member
Joined
Sep 15, 2011
Messages
7
Hi!

I would like to write code for a msgbox.

I have created a userform. I would like a msgbox to appear if the user has not entered thier first name into txtFirstName box.

First I would like to confirm that I write this code into the cmdSubmit section.

Secondly I need help to write the code.

This is what I have tried.

If txtFirstName = " " Then
msgbox "Your first name is required" vbcritical, "First Name"
End If

When I do this and click the submit button, no msgbox appears and the form continues to submit.

Thank you so much in advance-Josnow
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try

Code:
If txtFirstName = "" Then
msgbox "Your first name is required" vbcritical, "First Name"
End If

(no space between "")
 
Upvote 0
You also need a comma after your message

If txtFirstName = "" Then
msgbox "Your first name is required" vbcritical, "First Name"
End If

Should be

If txtFirstName = "" Then
msgbox "Your first name is required", vbcritical, "First Name"
End If
 
Upvote 0
When I do this and click the submit button, no msgbox appears and the form continues to submit.

I think you may also need to have an 'Exit Sub' in there somewhere to get the form to stop submitting.

So use Trevors as above but add:

Code:
 If txtFirstName = "" Then
msgbox "Your first name is required"[COLOR=red],[/COLOR] vbcritical, "First Name"
[COLOR=Red][B]Exit Sub[/B][/COLOR]
End If
 
Upvote 0
Hi to you all that replied. Just wanted to say a really BIG THANKYOU form the bottom of my heart. I do hope you come back to read this because I am so grateful.

This is what I ended up writing and thank to you all it works!!(smilin')

If txtFName = "" Then
MsgBox "Your first name is required", vbCritical, "First Name"
Exit Sub
ElseIf txtFName = True Then
Range("firstName").Value = txtFName.Text
End If

josnow
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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