+ Reply to Thread
Results 1 to 3 of 3

userform textbox error trapping

Hybrid View

  1. #1
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    userform textbox error trapping

    Hello,
    I have a userforum with several textboxes in them. My first textbox asks for a phone number. I want the user to only be able to enter numbers and after the first 3 numbers it automatically adds in a "-" and again after the next 3 then stops them form entering more numbers after 4 more, so it looks like ### - ### - ####.

    I also have another textbox and I would like the user to be able to enter anyting except %&*@?:_-+= those symbols.

    Thanks,

    -Eric

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: userform textbox error trapping

    Try this,
    Private Sub TextBox1_Change()
    
        If Len(TextBox1) = 3 Then TextBox1 = TextBox1 & "-"
        If Len(TextBox1) = 7 Then TextBox1 = TextBox1 & "-"
        If Len(TextBox1) = 12 Then MsgBox "Enough Already": TextBox2.SetFocus'goes to the next textbox when enough numbers are entered
    
    End Sub
    
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Else
            KeyAscii = 0
        End Select
        
    End Sub
    The keypress code can be found here for additional details
    http://www.cpearson.com/excel/TextBox.htm

  3. #3
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: userform textbox error trapping

    Thanks Dave works like a charm

+ 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