0

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
BigBen
  • 46,229
  • 7
  • 24
  • 40
Cameron Cole
  • 410
  • 1
  • 4
  • 20

1 Answers1

2

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.

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Tried this before as well, doesn't seem to do anything – Cameron Cole Jul 27 '23 at 15:46
  • Ah wait, `IsEmpty` is not appropriate here. Let me revise. – BigBen Jul 27 '23 at 15:47
  • Seems to work, can I ask why IsEmpty doesnt work in this situation? – Cameron Cole Jul 27 '23 at 15:49
  • https://stackoverflow.com/questions/14108948/excel-vba-check-if-entry-is-empty-or-not-space. Maybe the [IsEmpty](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/isempty-function) docs as well. – BigBen Jul 27 '23 at 15:49
  • 2
    To further his ask, I would also add `Exit Sub` after his msgbox so the macro doesn't continue. – cgwoz Jul 27 '23 at 16:02