Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Textbox Content Type


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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default Textbox Content Type

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default Textbox Content Type

Use the KeyPress event of the textbox

If KeyAscii < 48 Or KeyAscii 57 Then KeyAscii = 0

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default Textbox Content Type

Sorry, KingG, I read your post as "InputBox" instead of "Textbox".
Aidan's solution looks promising.
Zone

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Textbox Content Type

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
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
Copy a cell content into a TextBox snaggy^^ Excel Programming 1 January 19th 06 10:40 PM
Verify the format of the content of a textbox included in a frame Edgar[_2_] Excel Programming 1 October 13th 05 12:15 AM
Excel VBA - Cursor disappears when validating content of a TextBox lonewolfbr Excel Programming 0 September 16th 04 09:49 PM
Textbox Enabled but not where you can type in it Todd Huttenstine[_2_] Excel Programming 1 January 21st 04 12:18 AM
Type of textbox on userform Lieven Mettepenningen[_2_] Excel Programming 2 October 15th 03 01:36 PM


All times are GMT +1. The time now is 06:54 PM.

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

About Us

"It's about Microsoft Excel"