UserForm Textbox date between TODAY and 60 days earlier

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
On my user form, I would like the input limited to between TODAY and 60 days earlier.
I know how to write a formula for this on a worksheet, but not in a Userform.

Thank you for your help.

VBA Code:
    Private Sub txt_date_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        'If txt_date = vbNullString Then Exit Sub
    
        If IsDate(txt_date) Then
            txt_date = Format(txt_date, "Short Date")
        Else
            MsgBox "Not a valid date, try again!"
            Cancel = True
            txt_date.Value = ""
            frm_dataInput.txt_date.SetFocus
        End If
 
    End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Cancel = True is supposed to cancel moving on and keep the focus where it is but have always had trouble with it in the Exit event
as it seems focus leaves the text box anyway and when/if can't be found (even when you try to set focus) I think it's the form itself that ends up with focus.
I would use BeforeUpdate
VBA Code:
Private Sub txt_date_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    ' allow to continue if blank
    If txt_date = "" Then Exit Sub
    ' check what was entered
    If IsDate(txt_date) Then
        If DateValue(txt_date) <= Date And DateValue(txt_date) >= Date - 60 Then
            ' it's an OK date
            txt_date = Format(txt_date, "Short Date")
        Else
            ' it's not an OK date
            MsgBox "Not a valid date, try again!"
            Cancel = True
            txt_date.Value = ""
        End If
    Else
        ' it's not a date
        MsgBox "Sorry, that's not a date"
        Cancel = True
        txt_date.Value = ""
    End If
End Sub

Hope that helps
 
Upvote 0
Hi,
Try this, Add to your code
VBA Code:
            Dim UzerDt As Date
            Dim CurrDt As Date
            Dim DtDif As Single
            UzerDt = CDate(txt_date.Value)
            CurrDt = Date
            If CurrDt - UzerDt > 60 Then
                MsgBox "Ivalid date, Over-Limit!"
                Cancel = True
                txt_date.Value = ""
                frm_dataInput.txt_date.SetFocus
            End If
Add Code.png
 
Upvote 0
@Sahak
you should check your suggestion for the possibility of tomorrow's date being entered
 
Upvote 0
Hi NoSparks, thank you for your reply.
He was asking for between TODAY and 60 days earlier
Power Query:
 I would like the input limited to between TODAY and 60 days earlier

 
Upvote 0
He was asking for between TODAY and 60 days earlier
That's right and that's what you programmed for, in the earlier direction only.
No date in the future will be between TODAY and 60 days earlier
and therefore should also display the invalid date message.
 
Upvote 0
OK, let's add another "If" :)
VBA Code:
           If CurrDt < UzerDt Then
                MsgBox "Invalid date, Grater than current!"
                Cancel = True
                txt_date.Value = ""
                frm_dataInput.txt_date.SetFocus
            End If
complete VBA code will be
VBA Code:
If IsDate(txt_date) Then
            txt_date = Format(txt_date, "Short Date")
            
            Dim UzerDt As Date
            Dim CurrDt As Date
            Dim DtDif As Single
            UzerDt = CDate(txt_date.Value)
            CurrDt = Date
            If CurrDt - UzerDt > 60 Then
                MsgBox "Invalid date, Over-Limit!"
                Cancel = True
                txt_date.Value = ""
                frm_dataInput.txt_date.SetFocus
            End If
            If CurrDt < UzerDt Then
                MsgBox "Invalid date, Grater than current!"
                Cancel = True
                txt_date.Value = ""
                frm_dataInput.txt_date.SetFocus
            End If
        Else
            MsgBox "Not a valid date, try again!"
            Cancel = True
            txt_date.Value = ""
            frm_dataInput.txt_date.SetFocus
        End If
    End Sub
Is it OK now?
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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