PDA

View Full Version : Compare Dates within Textboxes in Userform



Loss1003
02-24-2016, 02:54 PM
Need help figuring out how to compare dates within textboxes. Having problems when certain textboxes are blank.




Sub Tdater3()
If UserForm1.Order1.Value = "" Then
UserForm1.Days3.Value = ""
Exit Sub
End If

If UserForm1.Date2.Value And UserForm1.Order1.Value = True Then
UserForm1.Days3.Value = CDate(UserForm1.Date2.Value) - CDate(UserForm1.Order1.Value)
End If

If UserForm1.Date2.Value = False And UserForm1.Order1.Value = True Then
UserForm1.Days3.Value = CDate(UserForm1.TDate1.Value) - CDate(UserForm1.Order1.Value)
End If

SamT
02-24-2016, 04:46 PM
Option Explicit

Sub Tdater3()

With UserForm1
If .Order1 = "" Then 'Order1.Value is False right now. 0 or "" = False
.Days3 = ""
Exit Sub
End If

'Order1 is not 0 or ""
If .Date2 Then 'Date2 is not 0 or ""
.Days3 = CDate(.Date2) - CDate(.Order1)
Else 'Date2 is 0 or ""
If .Tdate1 Then 'Checking if TDate1 is empty
.Days3 = CDate(.Tdate1) - CDate(.Order1)
End If
End If
End With
End Sub

Loss1003
02-25-2016, 07:48 AM
I received a run-time error '5' Invalid Procedure call or argument for the line starting at

If .Date2 Then 'Date2 is not 0 or ""

The textbox field is currently blank and will sometimes be blank or have a date entered.

SamT
02-25-2016, 11:28 AM
Use If .TextBox <> "" Then for all TextBoxes used in IF Functions. You can also use If Len(.TextBox) Then

Dates are stored and handled internally as Type Double. A Date of "" or 0 = 0.0 0 internally and is displayed as 01/01/1904 00:00:00. Times are the decimal portion of the Date Value. If Date = 0.25 then Time = 6AM. (24 x 0.25) Minutes and seconds requires a bit more math. IMO, the easiest is to use the Format Function: Minutes = CInt(Format(Date, "mm"))

Boolean False = 0 and True is any other number.

TextBox values are Strings. (tbxDate2.Text)

Apparently, VBA has trouble converting Strings to a number for the Boolean IF Function