+ Reply to Thread
Results 1 to 6 of 6

UserForm Textbox Specific Format

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    UserForm Textbox Specific Format

    Please can somebody help me.

    I have created a UserForm in Excel which is complete apart from one small problem.

    I have a text box into which I want the user to enter only 16 characters and I have set the max length property for this. But I also would like the data to be entered as follows, 3 Alpnumeric characters, 12 numeric characters, a “-“ and then 4 numeric characters e.g. AB-20101216-0001

    If the data input does not match this criteria then a message box should appear.

    Thank you.
    Last edited by AlexRoberts; 12-19-2010 at 06:28 AM. Reason: Question Solved

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: UserForm Textbox Specific Format

    This perhaps?
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    If Not Me.TextBox1.Value Like "???############-####" Then
        MsgBox "No"
        Cancel = True
    End If
    
    End Sub

  3. #3
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: UserForm Textbox Specific Format

    Thank you Stephen.

    I modified the code to "AB-########-#####" and it works a dream.

    Because I want to make things neat and tidy is there a way to postion the cursor back to the beginning of the string when the input does not match the criteria.

  4. #4
    Valued Forum Contributor jwright650's Avatar
    Join Date
    12-10-2010
    Location
    Va, USA
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    606

    Re: UserForm Textbox Specific Format

    Quote Originally Posted by AlexRoberts View Post
    Because I want to make things neat and tidy is there a way to postion the cursor back to the beginning of the string when the input does not match the criteria.
    Yes you can, Set the focus to the textbox.
    Try Adding this (see below) and see if that works for you.



    Me.TextBox1.SetFocus
    Last edited by jwright650; 12-18-2010 at 07:30 AM. Reason: added code tags
    Life is like a roll of toilet paper. The closer it gets to the end, the faster it goes.
    John Wright

  5. #5
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: UserForm Textbox Specific Format

    hi,

    In addition to Stephen's & John's suggestions, try playing around with the "textbox1.sel..." properties, for example:
    Option Explicit
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        With Me.TextBox1
            If Not .Value Like "???############-####" Then
                MsgBox "No"
                .SetFocus
                .SelStart = 3
                .SelLength = Len(Me.TextBox1) - 3
                Cancel = True
            End If
        End With
    End Sub

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: UserForm Textbox Specific Format

    I prefer not to bother users with messageboxes as if they did something 'wrong'. It also slows the ease with which a user uses your application.

    Based on the example in your first post:

    Private Sub TextBox1_Change()
    '    Application.EnableEvents = False
    
      With TextBox1
        Select Case Len(.Text)
        Case 1, 2
          .Text = UCase(.Text)
          If Asc(Right(.Text, 1)) < 65 Or Asc(Right(.Text, 1)) > 91 Then .Text = Left(.Text, Len(.Text) - 1)
          If Len(.Text) = 2 Then .Text = .Text & "-"
        Case 4 To 11, 13 To 16
          If Val(Right(.Text, 1)) = 0 Then .Text = Left(.Text, Len(.Text) - 1)
          If Len(.Text) = 11 Then .Text = .Text & "-"
        End Select
      End With
    
    '    Application.EnableEvents = False
    End Sub
    Last edited by snb; 12-18-2010 at 08:35 AM.



+ 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