Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Text Box - Restricting Entry

I want to restrict the entries that are allowed in a text box to just the
numbers 1 to 11.

I have fiddled around with the KeyDown, KeyUp, and KeyPress events, only
allowing entries with ASCII codes between 48 and 56.

However I cannot get it right. Can any expert VB programmers please supply
the necessary code.

Paul Smith


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Text Box - Restricting Entry

Paul,

There may be better ways of doing this but the following seems to work...
'------------------------------------
Private Sub TextBox1_Change()
If Val(TextBox1.Value) = 0 Then
TextBox1.Value = vbNullString
ElseIf Len(TextBox1.Value) 1 Then
If Val(TextBox1.Value) 11 Then TextBox1.Value = Left$(TextBox1.Value, 1)
End If
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim strChar As String
strChar = Chr$(KeyAscii)
If strChar Like "[!0-9]" Then KeyAscii = 0
End Sub
'------------------------------------

Regards,
Jim Cone
San Francisco, CA

"Paul W Smith" wrote in message ...
I want to restrict the entries that are allowed in a text box to just the
numbers 1 to 11.

I have fiddled around with the KeyDown, KeyUp, and KeyPress events, only
allowing entries with ASCII codes between 48 and 56.
However I cannot get it right. Can any expert VB programmers please supply
the necessary code.
Paul Smith


  #3   Report Post  
Posted to microsoft.public.excel.programming
mrt mrt is offline
external usenet poster
 
Posts: 70
Default Text Box - Restricting Entry

Paul i think the next code would do the trick, if i understand your problem correctly

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'only accept nummbers from 0 to 9
If KeyAscii 47 And KeyAscii < 58 Then
'In case nothing is in textbox not alouwd to put in 0
If KeyAscii = 48 And TextBox1.Text = "" Then
KeyAscii = 0
End If
'If there is already a number in the textbox
If Len(TextBox1.Text) = 1 Then
'The number in the textbox is a 1
If TextBox1.Text = 1 Then
'only 1 and 0 are ok
If KeyAscii 49 Then
KeyAscii = 0
End If
Else
KeyAscii = 0
End If
End If
'if there are already 2 numbers in the box then this is the maximum
If Len(TextBox1.Text) = 2 Then
KeyAscii = 0
End If
Else
'It is not a number
KeyAscii = 0
End If
End Sub

Good Luck

"Paul W Smith" wrote:

I want to restrict the entries that are allowed in a text box to just the
numbers 1 to 11.

I have fiddled around with the KeyDown, KeyUp, and KeyPress events, only
allowing entries with ASCII codes between 48 and 56.

However I cannot get it right. Can any expert VB programmers please supply
the necessary code.

Paul Smith



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
Restricting entry scubas Excel Worksheet Functions 1 January 11th 10 02:34 PM
Restricting data entry to A-Z a-z 0-9 Illya Teideman Excel Discussion (Misc queries) 10 August 28th 07 07:33 PM
Restricting Duplicate Entry Rajat Excel Worksheet Functions 5 November 7th 06 03:00 AM
Restricting entry in B1 on the basis of entry in A1 Stilla Excel Worksheet Functions 7 December 3rd 05 09:17 PM
Restricting entry in B1 on the basis of entry in A1 Biff Excel Worksheet Functions 0 December 3rd 05 03:41 AM


All times are GMT +1. The time now is 11:19 AM.

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

About Us

"It's about Microsoft Excel"