Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation - Weekend Or text jnk101 Excel Worksheet Functions 3 September 24th 05 09:02 AM
Text Validation Paul K. Excel Programming 2 January 20th 05 10:02 PM
Validation (Drop down list vs simple text length validation) Bob Phillips[_6_] Excel Programming 2 April 27th 04 07:47 PM
Validation (Drop down list vs simple text length validation) Jason Morin[_2_] Excel Programming 1 April 27th 04 04:56 PM
Validation (Drop down list vs simple text length validation) Pete McCosh Excel Programming 0 April 27th 04 03:49 PM


All times are GMT +1. The time now is 07:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"