View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Data Validation for TextBoxes

You can use the code below to restrict text entry in TextBox1 to the
characters "0" to "6". Any other character is rejected and won't be
placed in the text box.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("6")
' OK
Case Else
KeyAscii = 0
Me.Label1.Caption = "Illegal character."
End Select
End Sub

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Sat, 3 Jan 2009 13:58:00 -0800, ryguy7272
wrote:

I’m trying to figure out a way to add Data Validation for TextBoxes. I’ve
used this technique befo
If TextBox1 = "" Then
MsgBox "Please enter a value for 'Name'!!"
Exit Sub
End If

However, I’m not sure how to limit a TextBox to a range of numbers, such as
1-6. How can this be done? Also, how can I do it for many TextBoxes,
without specifically adding code for each?

A for Each…Next should do it, but I may need to add Data Validation for
several TextBoxes, such as 1-20, and then apply a slightly different set of
Data Validation rules for other TextBoxes, such as 1-4 for TextBoxes 21-25.
How would I go about doing that?

Thanks!!
Ryan---