Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default restrict textbox on userform to mm/yyyy

Does anyone know how to restrict a textbox on a userform to a date in
the format mm/yyyy? Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default restrict textbox on userform to mm/yyyy

You could try the following set of event code procedures for the TextBox to
control the user's input. NOTE that, as I have written the code, the user
does not have to (actually, they can't) type in the slash... my code will
enter it automatically for them. The user will be able to leave the TextBox
**only** if the TextBox is empty or if it contains a valid date (two-digit
month/four-digit year). In addition, the user will only be able to type
digits into the TextBox... no other characters can be entered. If you have
other required event code of your own that use event procedures I have
employed below, then you will have to integrate your required code into my
code. To implement my code, just copy/paste all of the code between the
START and END OF CODE markers into the UserForm's code window.

'*************** START OF CODE ***************
Dim LastPosition As Long

Private Sub TextBox1_Change()
Dim Cursor As Long
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
SecondTime = True
With TextBox1
If .Text Like "*[!0-9/]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
Cursor = .SelStart
Do While InStr(.Text, "/")
.SelStart = InStr(.Text, "/") - 1
.SelLength = 1
.SelText = ""
Cursor = Cursor - 1
Loop
If Len(.Text) 2 Then
.SelStart = 2
.SelText = "/"
Cursor = Cursor + 1
End If
.SelStart = IIf(Cursor < 0, 0, Cursor)
LastText = .Text
LastPosition = Cursor
End If
End With
SecondTime = False
End If
' Place any other Change event checking code here
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
With TextBox1
LastPosition = .SelStart
' Place any other MouseDown code here
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
' Place any other KeyPress checking code here
End With
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) Then
Cancel = Len(TextBox1.Text) < 7 Or Not IsDate(TextBox1.Text)
End If
' Place any other Exit checking code here
End Sub
'*************** END OF CODE ***************

--
Rick (MVP - Excel)



"lcoreilly" wrote in message
...
Does anyone know how to restrict a textbox on a userform to a date in
the format mm/yyyy? Thanks.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
fill userform textbox from userform listbox clik event GregJG[_21_] Excel Programming 3 December 7th 08 04:47 PM
Restrict date format for UserForm Rob Excel Discussion (Misc queries) 2 December 19th 05 12:13 AM
Userform Date Formatting MM/DD/YYYY Mike Excel Programming 7 January 7th 05 05:46 PM
restrict entry in a textbox to a range of values Dillonstar[_3_] Excel Programming 5 October 30th 03 05:00 PM
UserForm TextBox to ActiveSheet TextBox over 256 characters Dan E[_2_] Excel Programming 1 July 28th 03 07:36 PM


All times are GMT +1. The time now is 07:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"