-
Solved: select case problem
hi all:
first time to site, and hopefully not the last. i have been working on a project and could use some help.
here is the jist of it. my source data is read in through a text box called txtUnlGross. what i am trying to do is to prevent any key other than 0-9 being entered.
my first attempt at this resulted in that it would function, but not alow any number with the 9 in it. for example, you could key in something like 10000 but you would get an error msg from 89 or 90 or 19 etc.
Anyone have any idea why i am getting this error?
Private Sub txtUnlGross_Change()
Select Case txtUnlGross.Value
Case 0 To 9
'do nothing
Case ""
'do nothing
Case Else
MsgBox ("This must be a number less than 15,000.")
txtUnlGross = ""
txtUnlGross.SetFocus
Exit Sub
End Select
txtUnlNet.Enabled = True
End Sub
thanks!
-
-
The code should be in Key press event as stated by malik641
code will be as
[vba]Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 ' for number 0-9 (in excel to find out code use this formula =code(9)
'do nothing
Case Else
MsgBox "only numericals are allowed"
KeyAscii = 0
End Select
End Sub
[/vba]
Always Mark your Thread as Solved the moment u got acceptable reply (located under Thread tools)
Practice this & save time of others in thinking for unsolved thread
-
This version ignores improper entries.
[vba]Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = KeyAscii * (47 < KeyAscii) * (KeyAscii < 58)
End Sub[/vba]
-
thanks all for the help and advice. worked like a charm.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules