+ Reply to Thread
Results 1 to 11 of 11

Macro to limit number of allowed characters in a dialog input field

  1. #1
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Macro to limit number of allowed characters in a dialog input field

    Hello all,
    I need to limit the number of allowed characters (actually values) in an input field within an input box

    The input box syntax looks as follows

    InputBox("If Code is needed for the file name, then enter code and click 'OK', else click 'Cancel'", "Optional Input", Environ("Code"))

    Input Box1.JPG

    User can optionally enter an 8 digit number in the format "12345678", but should not be able to enter >8 characters, but they can also simply skip that step (this is already working via the "Cancel" button)

    Thanks in advance for your help :-)

    Cheers,
    Claus

  2. #2
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Macro to limit number of allowed characters in a dialog input field

    I think that Inputbox is not so smart to restrict input. One option is check length of input after OK is pressed and if length is >8 then call InputBox again.
    Another alternative could be small UserForm with TextBox for input. TextBox element have property MaxLength, and it can be set 8 in your case.
    and what happens if I'll input 1234ABC8? Digits & Letters?

  3. #3
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Re: Macro to limit number of allowed characters in a dialog input field

    Thanks kasan,
    I think you are right - simple inputbox seems actually not to be smart enough :-)

    If, then the user should only input numbers, not letters. Assuming, this will not be covered by the pure check for number of characters, right?

    Hence, I will give it a try with a smarter userform / textbox

    Regards, Claus

  4. #4
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Macro to limit number of allowed characters in a dialog input field

    Right, additional check on "Only numbers" is needed.
    This could be done in Textbox event Exit. Like this:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Re: Macro to limit number of allowed characters in a dialog input field

    Thanks Kasan!!!
    very helpful indeed!
    Still struggling how to get the textbox content into a certain cell

    'Range ("G3") = TextBox1'
    thought this would work the same way as the inputbox command, but this doesn't work :-(

    You see, I am still learning :-)

  6. #6
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Macro to limit number of allowed characters in a dialog input field

    I'm still learning too
    Try this way:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

  7. #7
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Macro to limit number of allowed characters in a dialog input field

    Please Login or Register  to view this content.
    This code entered in textbox module with restrict entry to 8 numeric characters

  8. #8
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Re: Macro to limit number of allowed characters in a dialog input field

    very cool nigelog!!!

    This comes already close :-)
    Now I need to get the "OK" and the "Step Over" buttons to work - but this I will figure out (I hope :-)

    Thanks!!!

    Add: I tried it in different ways now, but obviously my vba skills are far away from being acceptable :-)
    In the attached screenshot you see, what I am trying to do. The Textbox itself works pretty cool it appears, I can put in an 8 digit number; no more digits are allowed - BUT the OK button and the "Step Over" button do not work.
    When user entered a value, upon clicking on OK system should write the value in cell G3 (or cell G3 should take the value from the inputbox) and then proceed with the rest of the macro; if user does not want to enter a value, the system should proceed with the macro w/o writing anything in cell G3

    Maybe too dumb - but I do not get the buttons to work :-(

    Appreciate every help :-)commandButtons actions1.JPG
    Last edited by kappa0815; 12-06-2016 at 11:30 AM. Reason: additional info and screenshot

  9. #9
    Registered User
    Join Date
    12-05-2016
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    7

    Re: Macro to limit number of allowed characters in a dialog input field

    Try this for your commandbuttons

    Please Login or Register  to view this content.
    also the code for the textbox could be greatly simplified by this
    just add a small label below the textbox that shows the errormessage

    Please Login or Register  to view this content.
    Attached Images Attached Images
    Last edited by TheAnalyticalWolf; 12-06-2016 at 11:58 AM.

  10. #10
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Re: Macro to limit number of allowed characters in a dialog input field

    Hello TheAnalyticalWolf,

    thanks so much!!

    Maybe a spupid question for an expert, but do I put this code into the userform code or into my main macro?

    Cheers, Claus

  11. #11
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Re: Macro to limit number of allowed characters in a dialog input field

    Hello TheAnalyticalWolf,

    thanks so much!!

    Maybe a spupid question for an expert, but do I put this code into the userform code or into my main macro?

    Cheers, Claus

  12. #12
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Re: Macro to limit number of allowed characters in a dialog input field

    Thanks a lot to all.

    So, the final code looks like this and works perfectly

    TextBox1
    Sub CommandButton1_Click()
    Me.Hide
    End Sub
    Sub CommandButton2_Click()
    Me.Hide
    End Sub

    Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(TextBox1.Text) = 0 Then
    Exit Sub
    End If
    If Not IsNumeric(TextBox1.Text) Then
    Label1.Caption = "Only Numbers allowed" 'prints a message in the label below textbox
    Cancel = True
    ElseIf Len(TextBox1.Text) > 8 Then
    Cancel = True
    Label1.Caption = "Maximum 8 Numbers" 'prints a message in the label below textbox
    Cancel = True
    End If
    End Sub


    within main macro

    UserForm1.Show 1 'show 1 needed to run this userform and to stay'

    ActiveSheet.Range("g2").Value = UserForm1.TextBox1
    Unload UserForm1

  13. #13
    Registered User
    Join Date
    12-02-2016
    Location
    Germany
    MS-Off Ver
    2010 and 2013
    Posts
    21

    Re: Macro to limit number of allowed characters in a dialog input field

    Thanks a lot to all.

    So, the final code looks like this and works perfectly

    TextBox1
    Sub CommandButton1_Click()
    Me.Hide
    End Sub
    Sub CommandButton2_Click()
    Me.Hide
    End Sub

    Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(TextBox1.Text) = 0 Then
    Exit Sub
    End If
    If Not IsNumeric(TextBox1.Text) Then
    Label1.Caption = "Only Numbers allowed" 'prints a message in the label below textbox
    Cancel = True
    ElseIf Len(TextBox1.Text) > 8 Then
    Cancel = True
    Label1.Caption = "Maximum 8 Numbers" 'prints a message in the label below textbox
    Cancel = True
    End If
    End Sub


    within main macro

    UserForm1.Show 1 'show 1 needed to run this userform and to stay'

    ActiveSheet.Range("g2").Value = UserForm1.TextBox1
    Unload UserForm1

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Allowed Limit of 8192 characters trouble with Macro
    By DaveLet in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 06-08-2018, 11:37 PM
  2. Limit input to ONLY Numeric input and not alpha characters
    By aresquare1 in forum Excel General
    Replies: 3
    Last Post: 08-25-2015, 09:38 AM
  3. Formula for a fixed number of characters allowed in a cell
    By sapfanatic.com in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 05-19-2015, 10:28 AM
  4. Need a min and max limit for characters in a userform field
    By nohero in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-04-2014, 02:34 PM
  5. Increase the number of characters allowed in a post
    By Tony Valko in forum Suggestions for Improvement
    Replies: 9
    Last Post: 11-22-2013, 12:44 PM
  6. [SOLVED] Limit characters in an Input Box
    By jking80 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2006, 11:20 PM
  7. a way to limit the characters allowed in a cell?
    By EmRit24 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-29-2005, 05:06 PM

Tags for this Thread

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