Mandatory field Userform creating a runtime error 13

freddie mitchell

New Member
Joined
Apr 14, 2011
Messages
43
Hello everyone,

I'd like to put a mandatory field in a functioning VBA userform. This is primarily to stop a runtime error 13 appearing if no value is entered. However, the mandatory field code I've added also brings up an error 13, instead of a message asking the user to input a value. Does anyone know how to get the mandatory field to code to run without the error 13 appearing? I've posted the code for the mandatory field below.

VBA Code:
Private Sub OKButton_Click()

'check start date is filled correctly

If StartDate.Value = "" Or "DD/MM/YY" Then
MsgBox "please fill in the start date", vbCritical
Cancel = True
Else
'starting insert row function
Call Insert_Row
End If
End Sub

If anyone can help with this that would be amazing!

Thank you and happy holidays,
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
7,045
Office Version
  1. 2019
Platform
  1. Windows
You need to repeat the test after the OR statement

Rich (BB code):
Private Sub OKButton_Click()

'check start date is filled correctly
If StartDate.Value = "" Or StartDate = "DD/MM/YY" Then
MsgBox "please fill in the start date", vbCritical
Cancel = True
Else
'starting insert row function
Call Insert_Row
End If
End Sub

or you could try using IsDate function

VBA Code:
Private Sub OKButton_Click()

'check start date is filled correctly
If IsDate(StartDate.Value) Then
MsgBox "please fill in the start date", vbCritical
Cancel = True
Else
'starting insert row function
Call Insert_Row
End If
End Sub

and see if this produces required result

not sure what Cancel = True is meant to be doing?

Dave
 
Last edited:
Solution

freddie mitchell

New Member
Joined
Apr 14, 2011
Messages
43
That's great it works perfectly now, thank you!

Ha, the cancel=true was inadvertently left in by me because I was trying several ways to fix the issue, evidently with no success until you came along and helped out!

Thanks again Dave for your help
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
7,045
Office Version
  1. 2019
Platform
  1. Windows
No worries glad update resolved your issue

You can replace Cancel = True with

VBA Code:
StartDate.SetFocus

to keep focus on the control

My alternative suggestion omitted Not Operator to work correctly

Rich (BB code):
If Not IsDate(StartDate.Value) Then

Dave
 
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,151,972
Messages
5,767,402
Members
425,410
Latest member
SmittyT

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