How to insert a character while the user is still typing

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Is there a way to insert a character while the user is still typing. For example if the user is entering the date, automatically insert either a "-" or "," or "/", or a "." after the first two characters. For this instance I just need to insert the character after the two digit month followed by either a two or four digit year.
For example: 06/2019(prefered format), 06-2019, 06.2019, etc....
I was able to achieve this within the exit event but not in the change event. Not sure why.
Here's my code for the exit event.
Code:
Private Sub txtbxExpDte_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   
    If Mid(txtbxExpDte, 3, 1) = "/" Then
       Exit Sub
    Else
        txtbxExpDte.Value = Left(txtbxExpDte, 2) & "/" & (Mid(txtbxExpDte, 3, 5))
    End If
End Sub

Thank You
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about
Code:
Private Sub TextBox1_Change()
With Me.TextBox1
   If Len(.Value) = 2 Then .Value = .Value & "/"
End With
End Sub
 
Upvote 0
Hello Pookiemeister,

The event you chose updates the text box after the user has made an entry. You need to examine the keys as they are entered into the text box and allow only numbers and some other non printing keys.

This code validates the entry as it is typed and allows Backspace, Tab, Enter key, Arrow keys, Numbers and Keypad numbers. All other keys are ignored. Remove your Exit event code and use this instead...

Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) '


    If Len(TextBox1) > 7 Then Exit Sub
    
    Select Case KeyCode
        Case Is = 8, 9, 13, 37 To 40, 48 To 57, 96 To 105
            If Len(TextBox1) = 2 Then TextBox1 = TextBox1 & "/"
        Case Else: KeyCode = 0
    End Select
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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