+ Reply to Thread
Results 1 to 14 of 14

Need a min and max limit for characters in a userform field

  1. #1
    Registered User
    Join Date
    05-27-2011
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2007
    Posts
    72

    Need a min and max limit for characters in a userform field

    What I need is for this field in my userform to only allow 5 digit numbers, no more, no less, no letters, no special characters. Must be a 5 digit number.

    I know I can limit the field to 5 chars with MaxLength in the properties but how do I enforce a minimum and make it numeric only?

    Thanks in advance!
    Last edited by nohero; 06-03-2011 at 10:50 AM.

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

    Re: Need a min and max limit for characters in a userform field

    You could use a spinbutton min 10000, max 99999, showing the result in a textbox:

    Please Login or Register  to view this content.
    or you can use a combobox, type dropdownlist:

    Please Login or Register  to view this content.
    Last edited by snb; 06-03-2011 at 10:31 AM.



  3. #3
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Need a min and max limit for characters in a userform field

    To validate a textbox to only contain 5 digits...between 00000 and 99999, inclusive...

    Create a function to test the contents
    • Paste the below code into a General Module:
    Please Login or Register  to view this content.

    Then...Use this code for the Exit event of your textbox
    (change the control name to match what you used):
    Please Login or Register  to view this content.

    Now...Upon exiting your textbox, the validation code will engage.
    If the input is not 5 digits, it will be erased and a warning will display.

    Is that something you can use?
    Last edited by Ron Coderre; 06-03-2011 at 10:01 AM.
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

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

    Re: Need a min and max limit for characters in a userform field

    @Ron,

    In your approach wouldn't this be sufficient ?

    Please Login or Register  to view this content.

  5. #5
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Need a min and max limit for characters in a userform field

    Quote Originally Posted by snb View Post
    @Ron,

    In your approach wouldn't this be sufficient ?

    Please Login or Register  to view this content.
    Untested, but....I think that code would allow "1.234"
    wouldn't it?

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

    Re: Need a min and max limit for characters in a userform field

    @Ron,

    Quite right, but

    Please Login or Register  to view this content.

  7. #7
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Need a min and max limit for characters in a userform field

    I tweaked my code to have it actually erase the invalid entry
    and to use a more concise regular expression:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Need a min and max limit for characters in a userform field

    Hi nohero,

    Find the attached that may do what you want. After lots of playing around with a TextBox1 on a userform and events this may do what you want. Click the button to show the userform and then you can't get out of the first textbox unless it is 5 characters long and it is a number.

    You need to go to VBA and look at the code behind the Userform to see what it is doing.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  9. #9
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Need a min and max limit for characters in a userform field

    Did you ever get your mind headed down the wrong path and have trouble getting back on track? Well, that's what happened to me.

    This code seems to work just fine:
    Please Login or Register  to view this content.
    ...and it doesn't use any regular expressions...just native VBA

  10. #10
    Registered User
    Join Date
    05-27-2011
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Need a min and max limit for characters in a userform field

    Thanks for the suggestions guys, I didn't try your first post snb because I wasn't sure that it would accept a number that begins with a zero and these serial numbers very well could.

    I took Ron's first code and added it to my list of checks, I also added a vbNullString command at the end so I already have it clearing the field.

    Works like a charm, you guys are great!

  11. #11
    Registered User
    Join Date
    06-03-2011
    Location
    Orlando, Florida
    MS-Off Ver
    Excel 2000
    Posts
    4

    Re: Need a min and max limit for characters in a userform field

    hey noHero-
    As far as I know there is no way to do a min by Properties Method. You will have to write a Macro to handle the input. If you need help with that let me know.

  12. #12
    Registered User
    Join Date
    05-27-2011
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Need a min and max limit for characters in a userform field

    Well I now see it could have been easier but why mess with what works? It's working as intended now and I'm far from an expert so even though my code is starting to get really messy I don't want to disturb something important that I don't understand (just copied from elswhere).

    So far so good, you guys are a HUGE help!

  13. #13
    Registered User
    Join Date
    10-04-2014
    Location
    Liverpool, England
    MS-Off Ver
    2010
    Posts
    1

    Re: Need a min and max limit for characters in a userform field

    Hi I have just found this side and this code looks exactly what im looking for except the first 4 characters will be A-Z (PGOS will be exact text) then there will be 6 further numbers).

    Is is possible your code could work like this?

    .Pattern = "[P][G][O][S][0-9][0-9][0-9][0-9][0-9][0-9]"

    Or

    .Pattern = "[A-Z][A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9]"

    Quote Originally Posted by Ron Coderre View Post
    To validate a textbox to only contain 5 digits...between 00000 and 99999, inclusive...

    Create a function to test the contents
    • Paste the below code into a General Module:
    Please Login or Register  to view this content.

    Then...Use this code for the Exit event of your textbox
    (change the control name to match what you used):
    Please Login or Register  to view this content.

    Now...Upon exiting your textbox, the validation code will engage.
    If the input is not 5 digits, it will be erased and a warning will display.

    Is that something you can use?

  14. #14
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Need a min and max limit for characters in a userform field

    alantee,
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.


    Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.
    Ben Van Johnson

+ 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