Christopher Warrington
Christopher Warrington

Reputation: 767

Using VBA to check if a date is between two dates

So, I have looked through many of the questions on this site to answer this question, and I believe that my code is correct, but it still doesn't verify correctly. I cannot figure out why though.

What I am trying to do:

I am in Excel with a User Form. I have a date input box and a calendar picker. When one is changed, the other updates. I am trying to verify that the date entered into the input box is a valid date and within a two years time span, and if it is then update the calendar picker.

Errors:

  1. If a wrong date is entered into the input box then the program errors out as it cannot update the calendar picker with an invalid date. (This is an old error and thus why I do the validation check)
  2. If any valid date is entered, it does not verify correctly in the "If" statement.

For instance:

If I enter 2/18/2016, it should see that it is a valid date, is later than the oldDate and more recent than the lateDate. Then update the calendar picker with the value of the input box. But, with this code it always resets the value of the input box and gives the message that it didn't verify.

Code:

Private Sub weekInput_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim tempDate
    Dim oldDate
    Dim lateDate

    tempDate = weekInput.Value
    oldDate = Date - 365
    lateDate = Date + 365

    'Doing this to try and error check if an invalid date is given. Doesn't work for some reason.
    If IsDate(tempDate) And tempDate > oldDate And tempDate < lateDate Then
        'Find date of that week's Monday
        weekPicker.Value = weekInput.Value
    Else
        weekInput.Value = weekPicker.Value
        MsgBox "Didn't verify"
    End If
End Sub

Upvotes: 4

Views: 43213

Answers (1)

Christopher Warrington
Christopher Warrington

Reputation: 767

Thank you to @RonRosenfeld for his answer in the comments on the question. Here is the corrected code which works. Added code that also shows an error message if the date doesn't validate by changing text color on the User Form from blending into the background to being red.

Changes Explained:

The first changes made was to declare what type of variable tempDate, oldDate, and lateDate were. Before, not having them declared made them Variant. This insures that they are recorded as a date type and thus can be compared as I was trying to do.

So:

Dim tempDate
Dim oldDate
Dim lateDate

Becomes:

Dim tempDate As Date
Dim oldDate As Date
Dim lateDate As Date

The second change is moving the IsDate() to its own If statement. This is due to the fact that if the inputted text was not a date, it would cause a fatal error before even reaching the other If statement as it was trying to save a non-date to a variable of date type. So tempDate = weekInput.Value became:

If IsDate(weekInput.Value) Then
    tempDate = weekInput.Value
End If

The rest remained the same. Except I deleted the MsgBox and made a nicer, less obtrusive error message by changing the color of an error message from blending into the background to being red by adding these lines of code:

dateError.ForeColor = &H80000004
dateError.ForeColor = &HFF&

Answer:

Here is the full code:

Private Sub weekInput_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim tempDate As Date
    Dim oldDate As Date
    Dim lateDate As Date

    If IsDate(weekInput.Value) Then
        tempDate = weekInput.Value
    End If

    oldDate = Date - 365
    lateDate = Date + 365

    If tempDate >= oldDate And tempDate <= lateDate Then
        weekPicker.Value = weekInput.Value
        dateError.ForeColor = &H80000004
    Else
        weekInput.Value = weekPicker.Value
        dateError.ForeColor = &HFF&
    End If
End Sub

Upvotes: 5

Related Questions