Reputation: 265
Not having a great day on the programming front. I cannot see why VBA cannot do the following sum.
Can you please assist.
I have 2 fields in a userform both with a date. All I want is to subtract them to work out the days in between
I have tried 2 variations of this.
ws1.Cells(mRow, 28).Value = TxtRecDate.Value - TxtDOD.Value
ws1.Cells(mRow, 28).Value = CInt(TxtRecDate.Value) - CInt(TxtDOD.Value)
Both are giving me run-time error 13 mismatch.
Can you please help?
Thanks,
Upvotes: 0
Views: 186
Reputation: 7891
You need to convert the text
in the textboxes to date
:
ws1.Cells(mRow, 28).Value = DateValue(TxtRecDate.Value) - DateValue(TxtDOD.Value)
Note this will cause Runtime Error 13: Type Mismatch
if the text doesn't represent a date correctly. You can handle this by testing the textbox values first:
If IsDate(TxtRecDate) And IsDate(TxtDOD) Then
ws1.Cells(mRow, 28).Value = DateValue(TxtRecDate.Value) - DateValue(TxtDOD.Value)
Else
MsgBox "Invalid dates entered", vbExclamation + vbOKOnly
End If
Upvotes: 2
Reputation:
You should test to see if both textboxes contain valid dates. Then use DateValue
to convert the String values into Date values.
If IsDate(TxtRecDate.Value) And IsDate(TxtDOD.Value) Then
ws1.Cells(mRow, 28).Value = DateValue(TxtRecDate.Value) - DateValue(TxtDOD.Value)
End If
Upvotes: 0