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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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:
Upvote 0
Solution
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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