ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   text box validation (https://www.excelbanter.com/excel-programming/375683-text-box-validation.html)

kastoras

text box validation
 
Hi everyone,

I want to validate a textbox in vba. I want a textbox to accept only
numbers, not text.

I would really appreciate any kind of help or hint


Lefkios

Bob Phillips

text box validation
 
Test it on input

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
Select Case KeyCode
Case 48 To 57:
Case Else: KeyCode = 0
End Select
End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"kastoras" wrote in message
...
Hi everyone,

I want to validate a textbox in vba. I want a textbox to accept only
numbers, not text.

I would really appreciate any kind of help or hint


Lefkios




kastoras

text box validation
 
Hi Bob,

Thanks a lot for your answer. works but not exactly as i want it to be.

a) i cannot delete what i've already typed in. Backspace is not working or
DELETE key.
b) I also want to accept decimals. i.e 10.3

Again thanks a lot for the qick reply

Lefkios

"Bob Phillips" wrote:

Test it on input

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
Select Case KeyCode
Case 48 To 57:
Case Else: KeyCode = 0
End Select
End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"kastoras" wrote in message
...
Hi everyone,

I want to validate a textbox in vba. I want a textbox to accept only
numbers, not text.

I would really appreciate any kind of help or hint


Lefkios





Tom Ogilvy

text box validation
 
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8 To 10, 13, 27 'Control characters
Case 45, 46 ' negative and period
If KeyAscii = 45 Then ' hypen/negative
If Len(Trim(TextBox1.Text)) 1 Then
Beep
KeyAscii = 0
End If
End If
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End Sub

if you use comma (instead of period) as a decimal separator, I believe it is
keycode 44

--
Regards,
Tom Ogilvy


"kastoras" wrote in message
...
Hi Bob,

Thanks a lot for your answer. works but not exactly as i want it to be.

a) i cannot delete what i've already typed in. Backspace is not working or
DELETE key.
b) I also want to accept decimals. i.e 10.3

Again thanks a lot for the qick reply

Lefkios

"Bob Phillips" wrote:

Test it on input

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal
Shift As Integer)
Select Case KeyCode
Case 48 To 57:
Case Else: KeyCode = 0
End Select
End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"kastoras" wrote in message
...
Hi everyone,

I want to validate a textbox in vba. I want a textbox to accept only
numbers, not text.

I would really appreciate any kind of help or hint


Lefkios







kastoras

text box validation
 
Tom,

Thank you very very much. It works like a charm!

"Tom Ogilvy" wrote:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8 To 10, 13, 27 'Control characters
Case 45, 46 ' negative and period
If KeyAscii = 45 Then ' hypen/negative
If Len(Trim(TextBox1.Text)) 1 Then
Beep
KeyAscii = 0
End If
End If
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End Sub

if you use comma (instead of period) as a decimal separator, I believe it is
keycode 44

--
Regards,
Tom Ogilvy


"kastoras" wrote in message
...
Hi Bob,

Thanks a lot for your answer. works but not exactly as i want it to be.

a) i cannot delete what i've already typed in. Backspace is not working or
DELETE key.
b) I also want to accept decimals. i.e 10.3

Again thanks a lot for the qick reply

Lefkios

"Bob Phillips" wrote:

Test it on input

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal
Shift As Integer)
Select Case KeyCode
Case 48 To 57:
Case Else: KeyCode = 0
End Select
End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"kastoras" wrote in message
...
Hi everyone,

I want to validate a textbox in vba. I want a textbox to accept only
numbers, not text.

I would really appreciate any kind of help or hint


Lefkios








All times are GMT +1. The time now is 07:01 AM.

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