Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA else if statement not working as intended

In my userfield I have 2 textboxes for dates; if one of them is empty I want an error message to notify the user that they have to enter dates in both textboxes before continuing. I tried:

If IsEmpty(UserForm1.TextBox2.Value) And Not IsEmpty(UserForm1.TextBox3.Value) Or IsEmpty(UserForm1.TextBox3.Value) And Not IsEmpty(UserForm1.TextBox2.Value) Then
    MsgBox "Please fill both date fields", vbInformation, "Date Range Error"
    End If

before my next steps in the macro but nothing happens.

Private Sub CommandButton1_Click()

If IsEmpty(UserForm1.TextBox2.Value) And Not IsEmpty(UserForm1.TextBox3.Value) Or IsEmpty(UserForm1.TextBox3.Value) And Not IsEmpty(UserForm1.TextBox2.Value) Then
MsgBox "Please fill both date fields", vbInformation, "Date Range Error"
End If


If UserForm1.ComboBox1.Value = "(Blank)" Then
Sheets("FormResults").Range("C5").ClearContents 'Clear Product Type
Else
Sheets("FormResults").Range("C5").Value = UserForm1.ComboBox1.Value 'Replace Product Type
End If
Sheets("FormResults").Range("C4").Value = UserForm1.TextBox1.Value 'Replace Store
Sheets("FormResults").Range("C3").Value = UserForm1.TextBox2.Value 'Replace Start Date
Sheets("FormResults").Range("D3").Value = UserForm1.TextBox3.Value 'Replace End Date

Unload Me
End Sub

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Simply:

If Len(UserForm1.TextBox2.Value) = 0 Or Len(UserForm1.TextBox3.Value) = 0 Then
    MsgBox "Please fill both date fields", vbInformation, "Date Range Error"
End If

As pointed out in this thread, you can also compare to vbNullString.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading