简体   繁体   中英

Excel - runtime error 13: Type mismatch?

I have this problem with an Excel Form and I don't get it. The form has 2 date fields Begin and End Date and 1 CHeckbox called Fixed Period.

The goal is when the checkbox is checked that the period is meant to be 1 year, hence End date is recalculated with Begindate as Base. The VBA code below results in error 13 Type Mismatch on the If statement.

I thought Dates should be converted with CDate or is that nog the problem? Any pointer appreciated

Private Sub BeginDatum_Change()
Static OldValue As String

With Me.BeginDatum
    Debug.Print "Value: "; .Value; " Old Value: "; OldValue
    If CDate(.Value) > CDate(EindDatum.Value) Or CDate(.Value) > Date Then
        Debug.Print "EindDatum " & EindDatum.Value & " is NOT Good"
            .Value = OldValue
    Else
        Sheets("Traject").Range("Begin") = .Value
        OldValue = .Value
        If (FixedPeriod) Then
           EindDatum.Value = Format(DateAdd("yyyy", 1, Me.BeginDatum.Value), "dd/mm/yyyy")
        End If
    End If
End With
End Sub

One of these ends up not being a date or is impossible to convert to date.

  1. CDate(.Value)
  2. CDate(EindDatum.Value)
  3. CDate(.Value)

You need to determine whether the inputs represent a valid date before trying to convert.

If IsDate(.Value) then
   'use the input
End if

Etc...

IsDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM