+ Reply to Thread
Results 1 to 14 of 14

Textbox to accept 12 digits

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-22-2008
    Location
    canada
    Posts
    123

    Textbox to accept 12 digits

    I need help ont the below code. I need to change this code to accept 12 digits. Please help.

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As
    MSForms.ReturnInteger)
    ****Range("A65536").End(xlUp).Offset(1, 0).Value = Chr(KeyAscii)
    ****TextBox1.Value = ""
    End Sub

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I'm not sure what your code is doing. Where is the textbox?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    07-22-2008
    Location
    canada
    Posts
    123
    This code is put into the TEXTBOX. TEXTBOX has to be created then the code is put into the code section

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello JanJan,

    If you want to restrict the TextBox to accept a maximum of 12 characters then set the MaxLength property to 12 then you won't need the event code.

    Sincerely,
    Leith Ross

  5. #5
    Forum Contributor
    Join Date
    07-22-2008
    Location
    canada
    Posts
    123
    The event is used to make to jump to another cell. If I remove the event and just put MaxLength property to 12, it only accepts 12 digits but does not input it automatically to a cell.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I know the code is in the textbox, but what type of textbox

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        With Me.TextBox1
            If Len(.Value) > 12 Then
                .Value = Empty
                .SetFocus
                Exit Sub
                End If
            End With
    End Sub

  7. #7
    Forum Contributor
    Join Date
    07-22-2008
    Location
    canada
    Posts
    123
    Thank you for the respond and for the code. I tried the code but no luck. I mean I could enter 12 digit number but that's it, it does not go anywhere.

    Type of textbox ? -- This textbox was created from Control Toolbox.

    The code I mentioned before is really really good but I just don't know how to make it to accept 12 digit.

    Would you like me to send you an attachment so that you have a good look of what I have been trying to accomplish ?

    Thank you again.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    What do you mean that it doesn't go anywhere?

    Attach a small example workbook.

  9. #9
    Forum Contributor
    Join Date
    07-22-2008
    Location
    canada
    Posts
    123

    Re: Please see attached file

    Please see attached file.
    Attached Files Attached Files

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    In my first post I asked where the textbox is, you never answered. Eventually from the attachment I see it is a Forms menu control on the worksheet.I've wasted so much time trying to help you because you didn't answer that request!

  11. #11
    Forum Contributor
    Join Date
    07-22-2008
    Location
    canada
    Posts
    123
    I did respond to your question about the textbox, I put "?" as I thought would indicate of "not too sure" and I also mentioned that it was created from Control Toolbox. I really appreciated your time and I am very sorry for the time you wasted figuring it out, because of my unfamiliarity of some of Excel functions or terminology. I did ask you if I could send you an attachment to resolve some confusion.

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I think yopu want thisa

    
    Private Sub TextBox1_LostFocus()
    If Len(Me.TextBox1.Value) = 12 Then
    Range("A65536").End(xlUp).Offset(1, 0).Value = Me.TextBox1.Value
    TextBox1.Value = ""
    End If
    
    End Sub

  13. #13
    Forum Contributor
    Join Date
    07-22-2008
    Location
    canada
    Posts
    123
    Hi Roy,

    I replaced the old code with the code you provided and it did accept 12 digit however it only registers it to the cell once I click on that cell. It does not register it when it reaches the 12th digit or even when I press enter key (which I do not have to). As you may notice on the attached file I sent, when a number is entered, it automatically registers it to the next empty cell below it. I really appreciated you time & effort on helping me figuring this out. I do believe that you can help me with this. Thank you.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try using the Change event

    Private Sub TextBox1_Change()
        If Len(Me.TextBox1.Value) = 12 Then
            Range("A65536").End(xlUp).Offset(1, 0).Value = val
            Me.TextBox1.Value = ""
        End If
    End Sub

+ 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