UserForm - If TextBox is blank, Error Message and return to form

rachel06

Board Regular
Joined
Feb 3, 2016
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hi!

I have a userform that opens when the workbook is opened, and I'm trying to put in some safeguards if the form isn't filled out completely.

I've gotten it to where the message boxes work as expected, but clicking OK just pushes the macro onward, and I'd like it to stop and return to the form so the user can return and fill it out completely.

What do I need to add to this? I'm sure it's something easy.

Code:
Private Sub cmdok_Click()

If txtname = "" Then
MsgBox "Please Enter Your Name"
Else
If txtrunid = "" Then
MsgBox "Please Enter Your RunID"
Else
If txtsaveloc = "" Then
MsgBox "Please Enter Your Save Location"
Else
Worksheets(1).Range("H4") = txtname.Value
Worksheets(1).Range("H5") = txtrunid.Value
Worksheets(1).Range("B35") = txtsaveloc.Value
End If
End If
End If
Unload Me

End Sub

Thank you!!!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi rachel06,

Replace your code with the below and it will exit if all of the fields have not been completed. Also created a string to join all of the fields not entered in to the one message box

FieldRequired.png


VBA Code:
Private Sub cmdok_Click()

Dim ErrText As String
ErrText = "Please complete following Fields..." & vbCr

If txtname = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Name"
Else
End If

If txtrunid = "" Then
ErrText = ErrText & vbCr & "-  Enter Your RunID"
Else
End If

If txtsaveloc = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Save Location"
Else
End If

If ErrText = "Please complete following Fields..." Then
Worksheets(1).Range("H4") = txtname.Value
Worksheets(1).Range("H5") = txtrunid.Value
Worksheets(1).Range("B35") = txtsaveloc.Value
Else
MsgBox ErrText, , "Fields Required!"
Unload Me
End If

End Sub
 
Upvote 0
Hi rachel06,

Replace your code with the below and it will exit if all of the fields have not been completed. Also created a string to join all of the fields not entered in to the one message box

View attachment 98395

VBA Code:
Private Sub cmdok_Click()

Dim ErrText As String
ErrText = "Please complete following Fields..." & vbCr

If txtname = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Name"
Else
End If

If txtrunid = "" Then
ErrText = ErrText & vbCr & "-  Enter Your RunID"
Else
End If

If txtsaveloc = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Save Location"
Else
End If

If ErrText = "Please complete following Fields..." Then
Worksheets(1).Range("H4") = txtname.Value
Worksheets(1).Range("H5") = txtrunid.Value
Worksheets(1).Range("B35") = txtsaveloc.Value
Else
MsgBox ErrText, , "Fields Required!"
Unload Me
End If

End Sub
Thanks for the fast response!!

I love the idea of combining all errors into one message box! However, the macro still continues once "ok" is click on the error box instead of returning to the form for completion. Any ideas?
 
Upvote 0
Sorry misread thought it was to stop if not fully entered, swap the code to this...


VBA Code:
Private Sub cmdok_Click()

Dim ErrText As String
ErrText = "Please complete following Fields..." & vbCr

If txtname = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Name"
Else
End If

If txtrunid = "" Then
ErrText = ErrText & vbCr & "-  Enter Your RunID"
Else
End If

If txtsaveloc = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Save Location"
Else
End If

If ErrText = "Please complete following Fields..." & vbCr Then
Worksheets(1).Range("H4") = txtname.Value
Worksheets(1).Range("H5") = txtrunid.Value
Worksheets(1).Range("B35") = txtsaveloc.Value
Unload Me
Else
MsgBox ErrText, , "Fields Required!"
End If

End Sub
 
Upvote 1
Solution
Sorry misread thought it was to stop if not fully entered, swap the code to this...


VBA Code:
Private Sub cmdok_Click()

Dim ErrText As String
ErrText = "Please complete following Fields..." & vbCr

If txtname = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Name"
Else
End If

If txtrunid = "" Then
ErrText = ErrText & vbCr & "-  Enter Your RunID"
Else
End If

If txtsaveloc = "" Then
ErrText = ErrText & vbCr & "-  Enter Your Save Location"
Else
End If

If ErrText = "Please complete following Fields..." & vbCr Then
Worksheets(1).Range("H4") = txtname.Value
Worksheets(1).Range("H5") = txtrunid.Value
Worksheets(1).Range("B35") = txtsaveloc.Value
Unload Me
Else
MsgBox ErrText, , "Fields Required!"
End If

End Sub
Brilliant. This forum is the best. Thanks so much!!!
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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