ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Textbox Content Type (https://www.excelbanter.com/excel-programming/361317-textbox-content-type.html)

KingG

Textbox Content Type
 

Hi all,

how can I in VBA to set the Content of a Textbox to accept only e.g
Numbers and Not letters?

Thank you in advanc

--
King
-----------------------------------------------------------------------
KingG's Profile: http://www.excelforum.com/member.php...fo&userid=3437
View this thread: http://www.excelforum.com/showthread.php?threadid=54153


Zone

Textbox Content Type
 
KingG,
This is kinda klunky but works fine for positive numbers or 0.

Sub InputNums()
Dim NumErr As Boolean, myInp As String, j As Integer
NumErr = True
While NumErr
myInp = InputBox("Enter number", "Input", myInp)
If myInp = "" Then Exit Sub
NumErr = False
For j = 1 To Len(myInp)
If Mid(myInp, j, 1) < "0" Or Mid(myInp, j, 1) "9" Then
NumErr = True
MsgBox "Enter numbers only"
Exit For
End If
Next j
Wend
End Sub


[email protected]

Textbox Content Type
 
Use the KeyPress event of the textbox

If KeyAscii < 48 Or KeyAscii 57 Then KeyAscii = 0


Zone

Textbox Content Type
 
Sorry, KingG, I read your post as "InputBox" instead of "Textbox".
Aidan's solution looks promising.
Zone


Stefano Gatto

Textbox Content Type
 
Enter this sub in your form:

Private Sub TextBox1_Change()

If Asc(Mid(TextBox1.Text, Len(TextBox1.Text), 1)) < Asc("0") Or
Asc(Mid(TextBox1.Text, Len(TextBox1.Text), 1)) Asc("9") Then
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
Beep
End If
End Sub

If you want to also accept decimals, exp10, and negative numbers then change
the condition to something like IsNumber()...

--
Stefano Gatto


"KingG" wrote:


Hi all,

how can I in VBA to set the Content of a Textbox to accept only e.g.
Numbers and Not letters?

Thank you in advance


--
KingG
------------------------------------------------------------------------
KingG's Profile: http://www.excelforum.com/member.php...o&userid=34375
View this thread: http://www.excelforum.com/showthread...hreadid=541539




All times are GMT +1. The time now is 11:34 PM.

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