Mimic ENTER on Userform TextBox

mr_g_99

Board Regular
Joined
Aug 11, 2015
Messages
58
I have a userform with several text boxes. One of these boxes is to enter a date, the default text is "mm/dd/yy" and if the entry isn't recognized as a date, the entry is cleared, mm/dd/yy and focus return to the box. However this only works when ENTER is keyed. If I use TAB it just tabs to the next textbox.
Is there a way to get the TAB key to mimic the ENTER key? This would just be for TextBox4 and not the whole form.
Thanks!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about this placed in your userform module. The message box can obviously be removed or changed...

VBA Code:
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox4 = "" Then Exit Sub
    If Not TextBox4.Text = Format(TextBox4, "mm/dd/yy") Then
        TextBox4.Text = ""
        TextBox4.SetFocus
        Cancel = True
        MsgBox "This Value Must Be A Date In the Proper Format"
    End If
End Sub
 
Upvote 0
Solution
How about this placed in your userform module. The message box can obviously be removed or changed...

VBA Code:
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox4 = "" Then Exit Sub
    If Not TextBox4.Text = Format(TextBox4, "mm/dd/yy") Then
        TextBox4.Text = ""
        TextBox4.SetFocus
        Cancel = True
        MsgBox "This Value Must Be A Date In the Proper Format"
    End If
End Sub
Works perfectly, much appreciated!
I modified a little to highlight the TextBox

VBA Code:
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox4 = "" Then Exit Sub
    If Not TextBox4.Text = Format(TextBox4, "mm/dd/yy") Then
        With TextBox4
            .Text = "mm/dd/yy"
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        Cancel = True
    End If
End Sub
 
Upvote 0
I like it, nice touch. I was happy to help and I am glad it worked for you.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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