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
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,
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
Use the KeyPress event of the textbox
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
Sorry, KingG, I read your post as "InputBox" instead of "Textbox".
Aidan's solution looks promising.
Zone
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
>
>
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks