IF THEN ELSE Help

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
Disregard...I was not aware that tabbing through a field, caused Excel to think there was a value. I knew a space did that. I experimented some more with the LEN function and found the solution.

I'm trying to throw some data validation in, and I'm struggling with this one for some reason. If the txt_DPDate field on a UserForm isn't entered (it does get tabbed through), I'm still getting the msgbox. What I want is, if the field is NOT empty AND is a date, to convert the format. If it is NOT empty AND is NOT a date, throw the msgbox. If it IS empty, move on, without the msgbox.

I've tried all 3 sets of code, and none seem to be working when I just tab through the field. What am I missing?

Code:
Private Sub txt_DPDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the DP Date value is completed with a date, the value is formatted as MM/DD/YY.  If _
the DP Date is "empty", nothing happens.  If the DP Date value is something other than a date, _
an error is thrown.
If Not IsEmpty(Me.txt_DPDate.Value) And IsDate(Me.txt_DPDate.Value) Then
    Me.txt_DPDate.Value = Format(Me.txt_DPDate.Value, "MM/DD/YY")
ElseIf IsEmpty(Me.txt_DPDate.Value) Then Exit Sub
    Else
        MsgBox "Please enter a valid date that the Client's Diet Plan was created, in MM/DD/YY format."
End If
'If IsEmpty(Me.txt_DPDate.Value) Then Exit Sub
'If Not IsEmpty(Me.txt_DPDate.Value) And IsDate(Me.txt_DPDate.Value) Then
'    Me.txt_DPDate.Value = Format(Me.txt_DPDate.Value, "MM/DD/YY")
'Else
'    MsgBox "Please enter a valid date that the Client's Diet Plan was created, in MM/DD/YY format."
'End If
'If Not Len(Me.txt_DPDate.Value) = 0 And IsDate(Me.txt_DPDate.Value) Then
'    Me.txt_DPDate = Format(Me.txt_DPDate, "MM/DD/YY")
'If Not IsEmpty(Me.txt_DPDate.Value) Then
'    MsgBox "Please enter a valid date that the Client's Diet Plan was created, in MM/DD/YY format."
'    If response = vbOK Then Me.txt_DPDate.SetFocus
'End If
'End If
End Sub
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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