View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Keith Willshaw Keith Willshaw is offline
external usenet poster
 
Posts: 170
Default User Form Controls


"Ben E" wrote in message
...
Hi all,

Is it possible within a excel user form to set a text box
to:

1) only accept numbers
2) only accept a set number of characters

Ie the user can only input a 4 number code.


Ben E


The best option is probably to use the textbox change event
the example below will only permit numbers to be entered


Private Sub TextBox1_Change()
Dim keyascii As Integer
If TextBox1.Text < "" Then
keyascii = Asc(Right(TextBox1.Text, 1))

If keyascii 47 And keyascii < 58 Then
' If its a number allow it
Else
' Else throw away the change
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)

End If
End If

End Sub