ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   User Form Controls (https://www.excelbanter.com/excel-programming/275621-re-user-form-controls.html)

Keith Willshaw

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






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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com