ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Disable/Prevent Letters or Numbers in textbox (https://www.excelbanter.com/excel-programming/335537-disable-prevent-letters-numbers-textbox.html)

johncassell[_9_]

Disable/Prevent Letters or Numbers in textbox
 

Hello, I'm sure you have answered this a thousand times but I done
little search and couldn't find what I was looking for.

What I am after is a text box which does not allow letters to b
entered into it. I have seen a few codes which check for letters afte
the box has been filled but I want one where the user can press
letter key and nothing happens in the box i.e it only responds t
numbers.

Any help would be greatly appreciated.

Thanks

Joh

--
johncassel
-----------------------------------------------------------------------
johncassell's Profile: http://www.excelforum.com/member.php...fo&userid=2501
View this thread: http://www.excelforum.com/showthread.php?threadid=39016


TommySzalapski[_9_]

Disable/Prevent Letters or Numbers in textbox
 

Your tickets are the keypress event and the sendkeys method.
Use some code like:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If Not (KeyAscii = 48 And KeyAscii <= 57) Then ' not a digit
Range("B17").Value = KeyAscii
SendKeys ("{BS}") 'send backspace
End If

End Sub

What it does is sends a backspace character immediately after any
non-digit character.


--
TommySzalapski
------------------------------------------------------------------------
TommySzalapski's Profile: http://www.excelforum.com/member.php...o&userid=25561
View this thread: http://www.excelforum.com/showthread...hreadid=390166


johncassell[_10_]

Disable/Prevent Letters or Numbers in textbox
 

Thanks very much for that Tommy, precisely what I was after and thank
for replying so fast

--
johncassel
-----------------------------------------------------------------------
johncassell's Profile: http://www.excelforum.com/member.php...fo&userid=2501
View this thread: http://www.excelforum.com/showthread.php?threadid=39016


davidm

Disable/Prevent Letters or Numbers in textbox
 

As a matter of general interest, this too works: validates onl
non-numeric characters.

Private Sub TextBox1_Enter()
Call chk
End Sub

Sub chk()
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "TextBox" Then
If IsNumeric(ctl) Then
ctl.Text = ""
End If
End If
Next
End Su

--
david
-----------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...fo&userid=2064
View this thread: http://www.excelforum.com/showthread.php?threadid=39016


Tom Ogilvy

Disable/Prevent Letters or Numbers in textbox
 
True, but then the question is does he mean just the digits 0 to 9

? isnumeric("02,21,14.23")
True
? isnumeric("35E1")
True
? isnumeric("34D1")
True


Also, why the Enter event?

--
Regards,
Tom Ogilvy

"davidm" wrote in
message ...

As a matter of general interest, this too works: validates only
non-numeric characters.

Private Sub TextBox1_Enter()
Call chk
End Sub

Sub chk()
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "TextBox" Then
If IsNumeric(ctl) Then
ctl.Text = ""
End If
End If
Next
End Sub


--
davidm
------------------------------------------------------------------------
davidm's Profile:

http://www.excelforum.com/member.php...o&userid=20645
View this thread: http://www.excelforum.com/showthread...hreadid=390166





All times are GMT +1. The time now is 02:24 PM.

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