+ Reply to Thread
Results 1 to 5 of 5

Textbox Content Type

  1. #1
    Registered User
    Join Date
    05-12-2006
    Posts
    2

    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 advance

  2. #2
    Zone
    Guest

    Re: 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. #3

    Re: Textbox Content Type

    Use the KeyPress event of the textbox

    If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0


  4. #4
    Zone
    Guest

    Re: Textbox Content Type

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


  5. #5
    Stefano Gatto
    Guest

    RE: 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1