Forced Data Entry on textbox in user form

JeremyA1976

Board Regular
Joined
Aug 3, 2015
Messages
59
I am trying to force the person doing input to enter something into a text field in a user form, in order to submit the information to the main tab. Currently, they can leave the text box blank and submit nothing into the space. There are certain fields that NEED information in order to process. Is this possible?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Put something like this in your script:
Code:
'Modified 5/7/18 9:20 PM EDT
If TextBox1.Value = "" Then
MsgBox TextBox1.Name & "  Is Empty" & vbNewLine & "I'm going to stop this script"
Exit Sub
Else
MsgBox TextBox1.Name & "  Has the value  " & TextBox1.Value & " Entered"
End If
 
Upvote 0
Thanks My Answer... That worked exactly how I wanted it. How would i work in an "or" statement to define a total of 4 text boxes that need to be filled out? If any of the 4 input text boxes are left blank, I want a similar message box to appear. Thanks a ton! I can always rely on this forum for the clearest explanation and easiest coding.
 
Upvote 0
Try this:
Assuming your Textboxes are Named Textbox1 And Textbox2 and Textbox3 and Textbox4
These are default names.
Use this script:

Code:
Private Sub CommandButton5_Click()
'Modified 5/8/18 6:30 AM EDT
Dim i As Long
Dim ans As String
Dim Ms As String
Dim Mss As String
Mss = "I will stop this script till all Textboxes are filled in"
Ms = "These TextBoxs are empty"
    For i = 4 To 1 Step -1
        If Controls("TextBox" & i).Value = "" Then ans = Controls("Textbox" & i).Name & vbNewLine & ans
    Next
If ans <> "" Then
    MsgBox Ms & vbNewLine & ans & vbNewLine & Mss
Else
MsgBox "Do This"
End If
End Sub
 
Upvote 0
Thanks again, worked like a charm. I just changed my textbox names to their default and changed the wording on the error message. I appreciate your help
 
Upvote 0
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
Thanks again, worked like a charm. I just changed my textbox names to their default and changed the wording on the error message. I appreciate your help
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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