VBA UserForm Text - Equal and Should be Less Than Specific Date

alyceinwonderland

New Member
Joined
Mar 14, 2021
Messages
10
Office Version
  1. 2007
Platform
  1. Windows
Hi, Good day. I have this VBA project where in I have to compare the dates in my userform. Please help. Kindly see my script below for your reference. Thank you in advance.

I need the txtBS.Text to be less than or equal the date of the txtdate.Text.

I tried inserting this code to my VBA below but did not work:

If txtBS.Text <= txtdate.Text Then
MsgBox "The date entered into the before date is greater than the application."


VBA Code:


Private Sub CommandButton4_Click()
On Error GoTo xlERR

If Trim(txtDN.Text) = "" Then GoTo xlERR

If Trim(txtCN.Text) = "" Then GoTo xlERR

If Trim(txtdate.Text) = "" Then
GoTo xlERR
ElseIf IsDate(txtdate.Text) = False Then
MsgBox "Invalid Date", vbCritical, "Input Data"
Exit Sub
End If

If Trim(txtAO.Text) = "" Then GoTo xlERR

If Trim(txtBS.Text) = "" Then
GoTo xlERR
ElseIf IsDate(txtBS.Text) = False Then
MsgBox "Invalid Date", vbCritical, "Input Data"
Exit Sub
End If


bLoading = True
Sheet2.Unprotect "CA"
Sheet2.Range("$M$9").Value = txtCN.Text
Sheet2.Range("$Z$3").Value = txtdate.Text
Sheet2.Range("$Z$4").Value = txtAO.Text
Sheet2.Range("$M$16").Value = txtBS.Text
bLoading = False

Sheet2.Protect "CA"


With ActiveWorkbook
For i = 1 To Sheets.Count
If (.Sheets(i).Name <> "MAIN") Then
.Sheets(i).Visible = True

End If

Next
If .Sheets("MAIN").Visible = True Then .Sheets("MAIN").Visible = False
If .Sheets("Data Validation").Visible = True Then .Sheets("Data Validation").Visible = False

Me.Hide
GoTo ProceedSave
.Sheets("CA").Activate
.Protect Password:="appleone"
End With
Exit Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi. If you are comparing dates in the textboxes, then I don't think you'd want the .Text result of your text boxes. You'd just want to leave them as their input values. You could also convert using the CDATE(), but I don't think that's necessary.

VBA Code:
Private Sub CommandButton4_Click()
If Me.txtBS <= Me.txtdate Then
    MsgBox "The date entered into the before date is BEFORE than the application."
Else
    MsgBox "The date entered into the before date is AFTER than the application."
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top