Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() 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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Use the KeyPress event of the textbox
If KeyAscii < 48 Or KeyAscii 57 Then KeyAscii = 0 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sorry, KingG, I read your post as "InputBox" instead of "Textbox".
Aidan's solution looks promising. Zone |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Copy a cell content into a TextBox | Excel Programming | |||
Verify the format of the content of a textbox included in a frame | Excel Programming | |||
Excel VBA - Cursor disappears when validating content of a TextBox | Excel Programming | |||
Textbox Enabled but not where you can type in it | Excel Programming | |||
Type of textbox on userform | Excel Programming |